NSubstitute:收到的电话错误地断言

时间:2017-02-14 08:17:39

标签: nsubstitute

我已经创建了这个测试:

[TestFixture]
public class UsersTests
{
    private Core.Kernel coreKernel;
    private Core.Configuration.ICoreConfiguration coreConfiguration;

    [SetUp]
    public void SetUp()
    {
        this.coreConfiguration = NSubstitute.Substitute.For<Core.Configuration.ICoreConfiguration>();
        this.coreKernel = NSubstitute.Substitute.For<Core.Kernel>(this.coreConfiguration);

        this.coreKernel.Initialize();
    }

    [Test]
    public void AddUserTest()
    {
        Core.Communication.Entities.UserIdentity receivedUserIdentity = new Core.Communication.Entities.UserIdentity("user1", "passwd1");

        ((Core.Communication.ICoreService)this.coreKernel).AddUserIdentity(receivedUserIdentity);
        this.coreKernel.Received(100).AddUser(Arg.Is<Core.Identity.UserIdentity>(u => u.UserId.Equals(receivedUserIdentity.UserId)));
    }
}

其中Core.Kernel是:

public partial class Kernel : Core.IKernel
{
    public Kernel(Configuration.ICoreConfiguration configuration)
            : this(configuration, null, Enumerable.Empty<Type>())
    {

    }

    public Kernel(Configuration.ICoreConfiguration configuration, Communication.ICoreService service, IEnumerable<Type> producerTypes)
    {
        if (configuration == null)
            throw new ArgumentException("configuration object must be provided", "configuration");

        if (producerTypes.Any(t => !t.IsAssignableFrom(typeof(Core.Extensibility.AbstractProducerPlugin))))
            throw new ArgumentException("All types must inherit from AbstractProducerPlugin", "plugins");


        this.state = KernelState.initializing;

        this.configuration = configuration;
        this.service = service ?? this;
        this.producerTypes = producerTypes;

        this.backends = new Dictionary<Core.Identity.DomainIdentity, Backend.Infrastructure.IBackend>();
    }

    internal virtual void AddUser(Core.Identity.UserIdentity userIdentity) {...}
}

然而,this.coreKernel.Received(100).AddUser(...未被调用100次,只有一次。我做错了什么?

我的意思是,我不打算拨打100 AddUser来电话。我正在检查AddUser应该被调用100次。因此,断言应该失败。

修改

猜猜这段代码(Core.IKernel.AddUserIdentity(...)实施):

public class Core.Kernel {
    public override void Core.IKernel.AddUserIdentity(UserIdentity userIdentity) {
        this.AddUser(userIdentity); <<----- AddUser(...) is called
    }
}

我认为问题与:

有关
  1. Core.Kernel实施Core.IKernelCore.IKernelAddUserIdentity(...)方法。
  2. 我在嘲笑Core.Kernel而不是嘲笑Core.IKernel
  3. 根据Core.IKernel.AddUserIdentity(...)方法实现AddUser应该达到。
  4. AddUserinternal virtual的{​​{1}}方法。它不是任何方法接口的实现。
  5. 我希望断言Core.Kernel在达到AddUser时被调用一次。

    关于嘲笑的其他问题:

    1. AddUserIdentity其中T是具体类 - &gt; For<T>方法被替换了吗?没有执行virtual个方法?
    2. virtual其中T是具体类 - &gt;该类的哪些部分被模拟(ForPartsOf<T>,覆盖了接口方法)?

1 个答案:

答案 0 :(得分:0)

只调用一次因为您只调用AddUser一次。 Received断言检查它被调用了多少次,它没有告诉NSubstitue调用你的方法100次,你需要手动执行:)

  

NSubstitute还为您提供了通过将整数传递给Received()来断言接收特定数量的调用的选项。如果替换者没有收到那么多匹配的呼叫

,这将抛出

http://nsubstitute.github.io/help/received-calls/