I am trying to set up expectations on methods of a mocked object in Moq. At the same time, using Ninject, I try to have the kernel return my set up mock whenever a caller wants the corresponding interface. For more clarity, here’s some pseudocode
Class Car {
Void buildChassis() {
Engine = ObjectBuilder.get<Iengine>()
Engine.performCheckup()
}
}
When testing buildChassis, I want to plug in a mocked Engine.
Mock<Iengine>().setup().etc.etc.etc
However, Moq doesn’t play nice with Ninject : I can’t achieve this. I’m wondering if there are any robust software packages around which integrate both DI and mocking. I can’t get the ninject.moq package working, nor does it seem to be mature.
I use and like Moq, but use a different IoC container than Ninject. I’ve never felt the need to integrate mocking/isolation and IoC container frameworks. I use constructor injection, then simply pass mock objects to my classes in the unit tests. Only production code uses the container.
IMHO the real issue here is that Car knows about ObjectBuilder. Rather than using the container as a service locator, why not do constructor injection? That way only one top-level class needs to know anything about the IoC container.
Testing Car in isolation is easy; create a mock IEngine and pass it to the constructor.
You can pass in a factory interface if you need to create the engine at a later time.
If you decide to change to a different IoC framework, you only need to change one or two top-level classes.