c# - NSubstitute 无法确定要使用的参数规范

标签 c# mocking nsubstitute

我使用 NUnit 和 NSubstitute 进行单元测试。我有以下内容:

public interface IDataProvider
{
    void Log(int tvmId, DateTime time, int source, int level, int eventCode, string message);
}

...

var fakeDataProvider = Substitute.For<IDataProvider>();
...
fakeDataProvider.Received().Log(
    Arg.Any<int>(),
    new DateTime(2000, 1, 1),
    0,
    0,
    0,
    null);

fakeDataProvider.Received() 抛出 AmbiguousArgumentException 并显示无法确定要使用的参数规范的消息。我在 SO 上找到了以下内容

Cannot determine argument specifications to use

这是相关的,但我不能在上面的代码中应用它。为什么上面的代码有歧义?我还能如何指定 Received() 它应该接受任何参数?

最佳答案

由于 Log 方法中有多个 int 参数,因此您必须为每个参数指定参数。

fakeDataProvider.Received().Log(
    Arg.Any<int>(),
    new DateTime(2000, 1, 1),
    Arg.Is(0),
    Arg.Is(0),
    Arg.Is(0),
    null);

关于c# - NSubstitute 无法确定要使用的参数规范,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36202435/

相关文章:

java - 是否不鼓励在同一领域使用 @Spy 和 @InjectMocks?

c# - NSubstitute 中的 TargetInvocationException

c# - NSubstitute 虚拟 getter 使用 ForPartsOf 返回替换抛出异常

.net - NSubstitute的限制是什么,特别是MOQ?

c# - C#中 '@'的所有用法是什么?

c# - 数据库首先 Entity Framework 更新 EDMX 但不更新 Model.tt

c# - 更改 selectIndexChange 上的公共(public) int 值

c# - 为什么 Moq 看不到在 .Select 方法中调用此方法?

.net - TypeMock如何模拟密封类和非虚方法?

c# - 如何绕过 RhinoMocks 模拟中方法的执行?