nsubstitute 收到调用特定对象参数
nsubstitute received called with specific object argument
我有一个 class 看起来像这样:
public myArguments
{
public List<string> argNames {get; set;}
}
在我的测试中,我这样做:
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));
但测试失败并显示此错误消息:
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*)
我猜是因为 .Equals()
但我不确定如何解决它?
在您的测试中,您将 myArguments
class 与 List<string>
.
进行比较
您应该将 myArguments.argNames
与 List<string>
进行比较,或者在 myArguments
中实现 IEquatable<List<string>>
。
此外,当你比较List<T>
时,你应该使用SequenceEquals
而不是Equals
。
第一个选项是:
_mockedClass.Received().CheckArgs(
Arg.Is<myArguments>(x => x.argNames.SequenceEqual(expectedArgNames)));
第二个是:
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);
}
}
我有一个 class 看起来像这样:
public myArguments
{
public List<string> argNames {get; set;}
}
在我的测试中,我这样做:
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));
但测试失败并显示此错误消息:
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*)
我猜是因为 .Equals()
但我不确定如何解决它?
在您的测试中,您将 myArguments
class 与 List<string>
.
您应该将 myArguments.argNames
与 List<string>
进行比较,或者在 myArguments
中实现 IEquatable<List<string>>
。
此外,当你比较List<T>
时,你应该使用SequenceEquals
而不是Equals
。
第一个选项是:
_mockedClass.Received().CheckArgs(
Arg.Is<myArguments>(x => x.argNames.SequenceEqual(expectedArgNames)));
第二个是:
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);
}
}