ninject / Ninject.MockingKernel

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

Setup a mocked injected object #30

Closed jeusdi closed 7 years ago

jeusdi commented 7 years ago

I've configured a MockingKernel in order to mock a dependency:

[TestFixture]
public class TestsFixture
{
    private NSubstituteMockingKernel IoCKernel;

    public UsersTests()
    {
        this.IoCKernel = new NSubstituteMockingKernel();
    }

    [SetUp]
    public void SetUp()
    {
        this.IoCKernel.Reset();
    }

    [Test]
    public void AddUserTest()
    {
        var mock = this.IoCKernel.Bind<Core.Configuration.ICoreConfiguration>().ToMock();
        mock.Setup( <<<<<<< How to substitute methods of this mocked objects??????
            m =>
            m.UserIdentities
                .Returns(new List<UserIdentity>() {new UserIdentity("user1")}
        );

        Core.Kernel coreKernel = this.IoCKernel.Get<Core.Kernel>();
    }
}

According to this documentation I need to call a Setup method in order to substitute methods. Nevertheless, Setup method is not available.

Any ideas?

dvygolov commented 7 years ago

You didn't read the example carefully! Look at this place:

//setup the mock
var barMock = _kernel.GetMock<IBar>();
barMock.Setup(mock => mock.GetContent()).Returns("mocked Content");

and here's your code:

var mock = this.IoCKernel.Bind<Core.Configuration.ICoreConfiguration>().ToMock();
mock.Setup(...);

You don't get the mock, you just declare the binding.

jeusdi commented 7 years ago

I'm about to suicide!

Currently:

var mock = this.IoCKernel
    .Bind<Core.Configuration.ICoreConfiguration>()
    .ToMock();

Nevertheless compiler is getting me that Setup method is not available!!

scott-xu commented 7 years ago

Please use GetMock to get mock object.

jeusdi commented 7 years ago

GetMock is not available!!! Could you provide an example using NSubstituteMockingKernel? Which usings do I need to add?