ninject / Ninject.MockingKernel

Extension for Ninject aiding testability in Moq, NSubstitute, and FakeItEasy
http://ninject.org/
Other
62 stars 25 forks source link

"Simple" example? #27

Closed KevinBurton closed 6 years ago

KevinBurton commented 8 years ago

I have a question on the "simple" example. Maybe it is just me but the example doesn't seem to clarify usage for me. I need just the basic setup for a test. The way I understand it is

1) Create an instance of MockingKernel() 2) The kernal will "automatically" mock all interfaces. I can get a specific instance of a mock implementation using the method GetMock() on the kernel. 3) Change the methods of the mocked interface implementations to return what is needed for testing.

I am unclear as to what the role of Reset() is and why in the example the interface is bound to the instance twice (once in the constructor and once via GetMock()).

Thank you.

philippdolder commented 8 years ago

Hi Kevin,

With GetMock you get the mocked instance so that you can configure the mock object (Setup method). Get<IFoo> will return an instance of MyFoo which receives the previously configured mock of IBar in its constructor.

Regarding the call to Reset: According to https://github.com/ninject/Ninject.MockingKernel/blob/master/src/Ninject.MockingKernel/MockingKernel.cs#L67 this deactivates all instances and empties the kernel's cache. i.e. this resets the kernel to a defined state, so that all objects will be created from scratch in the next test. Keep in mind that the example is written with NUnit. NUnit reuses instances of [TestFixture] for all [Test] methods. You could as well execute the logic from the constructor in the [SetUp] method, then the call to Reset would be unnecessary. Only using the [SetUp] method is my personally preferred way when working with NUnit.

I hope this clarifies the example for you.

Philipp

kevinkuszyk commented 8 years ago

@KevinBurton - I found I was repeating a lot of boiler plate code when using the MockingKernel, so I wrote a lightweight test framework on top of it.

With it, the simple example test class can be refactored to this:

[TestFixture]
public class MyTests : TestsFor<MyFoo>
{
    [Test]
    public void Test1()
    {
        // Arrange
        // setup the mock
        var barMock = GetMock<IBar>();
        barMock.Setup(mock => mock.GetContent()).Returns("mocked Content");

        // Act
        Subject.DoStuff(); // The Subject property gets the real MyFoo with a mocked IBar injected into it
                           // this will print "mocked Content", because the mocked object is called

        // Assert
        barMock.VerifyAll();
    }
}
behm commented 8 years ago

I am have trouble finding the GetMock() method for the FakeItEasyMockingKernel. Is there a secret to using that version. I have found no coding samples for FakeItEasy so I am trying to adapt the Moq samples.

philippdolder commented 8 years ago

@behm

FakeItEasy does not have a concept of a Mock class wrapping the mocked object. So with the FakeItEasyMockingKernel you will write something like

var bar = kernel.Get<IBar>();
A.CallTo(() => bar.Foo()).Returns(...);

to make a setup for example.