我已经创建了这个测试:
[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
}
}
我认为问题与:
有关Core.Kernel
实施Core.IKernel
。 Core.IKernel
有AddUserIdentity(...)
方法。Core.Kernel
而不是嘲笑Core.IKernel
。Core.IKernel.AddUserIdentity(...)
方法实现AddUser
应该达到。AddUser
是internal virtual
的{{1}}方法。它不是任何方法接口的实现。我希望断言Core.Kernel
在达到AddUser
时被调用一次。
关于嘲笑的其他问题:
AddUserIdentity
其中T是具体类 - &gt; For<T>
方法被替换了吗?没有执行virtual
个方法?virtual
其中T是具体类 - &gt;该类的哪些部分被模拟(ForPartsOf<T>
,覆盖了接口方法)?答案 0 :(得分:0)
只调用一次因为您只调用AddUser
一次。 Received
断言检查它被调用了多少次,它没有告诉NSubstitue调用你的方法100次,你需要手动执行:)
NSubstitute还为您提供了通过将整数传递给Received()来断言接收特定数量的调用的选项。如果替换者没有收到那么多匹配的呼叫
,这将抛出