Search code examples
ninjectnsubstitute

Setup a mocked injected object


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?

I've read documentation. I'm asking for some examples. Nevertheless, I think that documentation is a bit poor.

EDIT

I've tried this:

public void Test() {

    Core.Configuration.UserIdentity userConfiguration = Core.Configuration.UserIdentity.Create("u1", "p1");
    IEnumerable<Core.Configuration.UserIdentity> configurationUsers = new List<Core.Configuration.UserIdentity>() { userConfiguration };

    this.IoCKernel.Get<Core.Configuration.ICoreConfiguration>().UserIdentities.Returns(configurationUsers);

    //Testing
    Core.Kernel kernel = this.IoCKernel.Get<Core.Kernel>();
    kernel.Received(1).AddUser(Arg.Any<Core.Identity.UserIdentity>());
}

Nevertheless, I'm getting this NSubstitute.Exceptions.NotASubstituteException exception message now on last line:

NSubstitute extension methods like .Received() can only be called on objects created using Substitute.For() and related methods.

As you can see I'm trying to test AddUser method is called once at least. AddUser must be called according my Core.Kernel implementation.


Solution

  • Here's a working example:

    using Ninject;
    using Ninject.MockingKernel.NSubstitute;
    using NSubstitute;
    using NUnit.Framework;
    
    namespace ClassLibrary1
    {
        [TestFixture]
        public class MyTests
        {
            [Test]
            public void Test1()
            {
                using (var kernel = new NSubstituteMockingKernel())
                {
                    var substitute = kernel.Get<IDummyService>();
                    substitute.ReturnInt().Returns(1);
    
                    var instance = kernel.Get<DummyClass>();
                    Assert.AreEqual(1, instance.Calc());
                    substitute.Received(1).ReturnInt();
                }
            }
    
            public interface IDummyService
            {
                int ReturnInt();
            }
    
            public class DummyClass
            {
                private IDummyService _dummyService;
    
                public DummyClass(IDummyService dummyService)
                {
                    _dummyService = dummyService;
                }
    
                public int Calc()
                {
                    return _dummyService.ReturnInt();
                }
            }
        }
    }
    

    Using the following packages:

    PM> Get-Package
    
    Id                                  Versions                                 ProjectName                                                                                                                                                                           
    --                                  --------                                 -----------                                                                                                                                                                           
    Ninject                             {3.2.2.0}                                ClassLibrary1                                                                                                                                                                         
    Ninject.MockingKernel               {3.2.2.0}                                ClassLibrary1                                                                                                                                                                         
    Ninject.MockingKernel.NSubstitute   {3.2.2.0}                                ClassLibrary1                                                                                                                                                                         
    NSubstitute                         {1.10.0.0}                               ClassLibrary1                                                                                                                                                                         
    NUnit                               {3.6.0}                                  ClassLibrary1