簡體   English   中英

沒有指定輸入參數的Moq模擬方法

[英]Moq mock method with out specifying input parameter

我在使用Moq的測試中有一些代碼:

public class Invoice
{
    ...

    public bool IsInFinancialYear(FinancialYearLookup financialYearLookup)
    {
        return InvoiceDate >= financialYearLookup.StartDate && InvoiceDate <= financialYearLookup.EndDate;
    }
    ...
}

所以在單元測試中我試圖模擬這個方法並讓它返回true

mockInvoice.Setup(x => x.IsInFinancialYear()).Returns(true);

無論如何都要寫這一行,所以我不必指定IsInFinancialYear的輸入。 即。 所以它在代碼中沒有輸入參數是什么,它將返回true傳遞給它的什么?

您可以使用It.IsAny<T>()來匹配任何值:

mockInvoice.Setup(x => x.IsInFinancialYear(It.IsAny<FinancialYearLookup>())).Returns(true);

請參閱快速入門的“ 匹配參數”部分。

嘗試使用It.IsAny<FinancialYearLookup>()接受任何參數:

mockInvoice.Setup(x => x.IsInFinancialYear(It.IsAny<FinancialYearLookup>())).Returns(true);

您可以嘗試以下方法:

https://7pass.wordpress.com/2014/05/20/moq-setup-and-ignore-all-arguments/

允許:

mock
.SetupIgnoreArgs(x => x.Method(null, null, null)
.Return(value);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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