![](/img/trans.png)
[英]Match a func delegate in argument matcher in nsubstitute Received method
[英]nsubstitute received called with specific object argument
我有一個看起來像這樣的類:
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
類與List<string>
進行比較。
您應該比較myArguments.argNames
與List<string>
或實現IEquatable<List<string>>
在myArguments
。
此外,當您比較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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.