Moq mock method with out specifying input parameter

AnonyMouse picture AnonyMouse · Oct 19, 2011 · Viewed 67.3k times · Source

I have some code in a test using 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

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. ie. So that it doesn't in the code what the input parameter is it will return true whatever is passed to it?

Answer

Jeff Ogata picture Jeff Ogata · Oct 19, 2011

You can use It.IsAny<T>() to match any value:

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

See the Matching Arguments section of the quick start.