How to use Ninject.MockingKernel?

I am trying to use Ninject.MockingKernel.Moq. I have 2 problems:

  • I have to register all the types that I want to make fun of. If I do not, the constructor without parameters is called without my class, and that is not the goal of automocker
  • It seems that even if the layout is called, the check failed. Look at the following sample

Code example:

//Arrange
var kernel = new Ninject.MockingKernel.Moq.MoqMockingKernel();
kernel.Bind<ClassUnderTest>().ToSelf();
kernel.Bind<ILogger>().ToMock();
kernel.GetBindings(typeof(ILogger));
//Act
var sut = kernel.Get<ClassUnderTest>();
sut.DoSomething();//Logger.Log is called inside that method
//Assert
var mock = kernel.GetMock<ILogger>();
mock.Verify(x => x.Log(It.IsAny<string>()), Times.Exactly(1));
+3
source share
1 answer

For self-sustaining types, such as non-abstract classes, an instance of the class is returned by default. The goal is to simplify the use of the most common use case when a class decision is an object under testing and all dependencies are defined as interfaces.

, . , , .

, , .

,

// note the scope so that you can access it later again
kernel.Bind<Foo>().ToMock().InSingletonScope(); 
var mock = kernel.GetMock<Foo>()
+8

All Articles