简体   繁体   English

没有指定输入参数的Moq模拟方法

[英]Moq mock method with out specifying input parameter

I have some code in a test using Moq: 我在使用Moq的测试中有一些代码:

public class Invoice
{
    ...

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

So in a unit test I'm trying to mock this method and make it return true 所以在单元测试中我试图模拟这个方法并让它返回true

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

Is there anyway to write this line so I don't have to specify the input to IsInFinancialYear . 无论如何都要写这一行,所以我不必指定IsInFinancialYear的输入。 ie. 即。 So that it doesn't in the code what the input parameter is it will return true whatever is passed to it? 所以它在代码中没有输入参数是什么,它将返回true传递给它的什么?

You can use It.IsAny<T>() to match any value: 您可以使用It.IsAny<T>()来匹配任何值:

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

See the Matching Arguments section of the quick start. 请参阅快速入门的“ 匹配参数”部分。

尝试使用It.IsAny<FinancialYearLookup>()接受任何参数:

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

You can try the following: 您可以尝试以下方法:

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

Allows: 允许:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM