MockingKernel和Received抛出NotASubstituteException

时间:2017-02-16 09:28:44

标签: ninject

我正在使用NSubstituteMockingKernel来构建我的库类的所有依赖项。我一直在努力解决一周的麻烦,我真的很累。我需要一些帮助。

这是我的班级:

class Core : ICoreService, ICore {
    private ICoreConfiguration configuration;

    Core(ICoreconfiguration configuration) {
        this.configuration = configuration;
    }

    override ICoreService.ParentMethod() {   //ICoreService implementation
        foreach (Item item in this.configuration.Items)
            this.ChildMethod();
    }
    virtual ChildMethod() {
      //do something
    }
}

ICoreService是:

interface ICoreService {
    void ParentMethod();
}

ICore是:

interface ICore {
    ICoreConfiguration Configuration { get; }
}

ICoreConfiguration是:

interface ICoreConfiguration {
    IEnumerable<Item> Items { get; }
}

我的测试是:

[TestFixture]
public class UsersManagementTests
{
    private readonly NSubstituteMockingKernel IoCKernel;

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

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

    [Test(Description = "Configured Users are well loaded on Kernel")]
    public void InitializationWithUsersTest()
    {
        //Setup Data
        Item item = Item.Create("item1");
        IEnumerable<Item> items = new List<Item>() { item };

        //Setup Mocks
        this.IoCKernel
            .Get<ICoreConfiguration>()
            .Items
                .Returns(items);
        Core core = this.IoCKernel.Get<Core>();

        //Act
        kernel.ParentMethod();

        //Assert
        IEnumerable<NSubstitute.Core.ICall> calls = kernel.ReceivedCalls(); // ((((((1))))))
        kernel.Received(1).ChildMethod();                                                    // ((((((2))))))
    }
}

当达到((((((1))))))((((((2))))))时,我现在在最后一行收到此NSubstitute.Exceptions.NotASubstituteException异常消息:

  

NSRestitute扩展方法,如.Received()只能在使用Substitute.For()和相关方法创建的对象上调用。

正如您所看到的,我尝试至少尝试一次ChildMethod方法。必须根据我的ChildMethod实施方式调用Core.Kernel

我真的很感激一些帮助。

感谢。

0 个答案:

没有答案
相关问题