adam0101
adam0101

Reputation: 30995

Using Moq to mock some constructor arguments and letting Ninject take care of the rest?

My service takes a bunch of arguments in the constructor which are normally injected by Ninject. When writing a unit test, could I mock just one of those constructor arguments and still have Ninject do the rest?

Upvotes: 0

Views: 375

Answers (1)

BatteryBackupUnit
BatteryBackupUnit

Reputation: 13233

Yes you can do that. Of course you'll need registration for all other dependencies, too (from your production code or done "manually"). Just do

...instanciation of kernel and registration of non-mock types

var fooMock = new Mock<IFoo>();
kernel.Bind<IFoo>().ToConstant(fooMock.Object);

SomeTestee testee = kernel.Get<SomeTestee>();

If, however, IFoo would already be bound by production code, you could use Rebind to replace the binding:

kernel.Rebind<IFoo>().ToConstant(fooMock.Object);

Yet another alternative is to use the Ninject.MockingKernel. This provides a syntax like Bind<IService>().ToMock();. But you best look at the website since explaining it all here would go beyond the scope.

Upvotes: 1

Related Questions