nsubstitute收到调用特定对象参数

SOf*_*tic 4 c# unit-testing nsubstitute

我有一个看起来像这样的类:

public myArguments
{
    public List<string> argNames {get; set;}
}
Run Code Online (Sandbox Code Playgroud)

在我的测试中,我这样做:

var expectedArgNames = new List<string>();
expectedArgNames.Add("test");

_mockedClass.CheckArgs(Arg.Any<myArguments>()).Returns(1);

_realClass.CheckArgs();

_mockedClass.Received().CheckArgs(Arg.Is<myArguments>(x => x.argNames.Equals(expectedArgNames));
Run Code Online (Sandbox Code Playgroud)

但测试失败并显示以下错误消息:

NSubstitute.Exceptions.ReceivedCallsException : Expected to receive a call matching:
    CheckArgs(myArguments)
Actually received no matching calls.
Received 1 non-matching call (non-matching arguments indicated with '*' characters):
    CheckArgs(*myArguments*)
Run Code Online (Sandbox Code Playgroud)

我猜它是因为.Equals()但我不知道如何解决它?

Mar*_*ldi 5

在你的考试中,你要比较一个myArguments班级List<string>.

您应该比较myArguments.argNamesList<string>或实现IEquatable<List<string>>myArguments.

此外,当你进行比较时List<T>,你应该使用SequenceEquals而不是Equals.

第一种选择是:

_mockedClass.Received().CheckArgs(
    Arg.Is<myArguments>(x => x.argNames.SequenceEqual(expectedArgNames)));
Run Code Online (Sandbox Code Playgroud)

第二个是:

public class myArguments : IEquatable<List<string>>
{
    public List<string> argNames { get; set; }

    public bool Equals(List<string> other)
    {
        if (object.ReferenceEquals(argNames, other))
            return true;
        if (object.ReferenceEquals(argNames, null) || object.ReferenceEquals(other, null))
            return false;

        return argNames.SequenceEqual(other);
    }
}
Run Code Online (Sandbox Code Playgroud)