C# 不指定输入参数的Moq模拟方法

C# 不指定输入参数的Moq模拟方法,c#,mocking,moq,C#,Mocking,Moq,我在使用Moq的测试中有一些代码: public class Invoice { ... public bool IsInFinancialYear(FinancialYearLookup financialYearLookup) { return InvoiceDate >= financialYearLookup.StartDate && InvoiceDate <= financialYearLookup.EndDate

我在使用Moq的测试中有一些代码:

public class Invoice
{
    ...

    public bool IsInFinancialYear(FinancialYearLookup financialYearLookup)
    {
        return InvoiceDate >= financialYearLookup.StartDate && InvoiceDate <= financialYearLookup.EndDate;
    }
    ...
}
是否还要写这一行,这样我就不必指定
IsInFinancialYear
的输入。因此,它不会在代码中显示输入参数是什么,无论传递给它的是什么,它都会返回true?

尝试使用
it.IsAny()
接受任何参数:

mockInvoice.Setup(x => x.IsInFinancialYear(It.IsAny<FinancialYearLookup>())).Returns(true);
mockInvoice.Setup(x=>x.IsInFinancialYear(It.IsAny()).Returns(true);
您可以使用
It.IsAny()
匹配任何值:

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

请参阅快速入门部分。

您可以尝试以下操作:

允许:

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

我知道这个答案很古老,但如果我有一个以上的简单参数呢?是否可以只说“类型适合所有参数的任何内容”@Brandon然后,对于每个参数,您都有一个it.IsAny(),其中类型是param的任何类型。如果您愿意,您可能可以编写一个助手函数,通过反射为您执行此操作。同意此处的其他注释:为任何非平凡的方法键入此命令都是一件非常痛苦的事情。任何人都有这样做的助手吗?或者您必须为每个方法编写一个helper/Pull请求,以请求Moq扩展:-)
mock
.SetupIgnoreArgs(x => x.Method(null, null, null)
.Return(value);