簡體   English   中英

nsubstitute收到調用特定對象參數

[英]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.argNamesList<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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM