AnonyMouse
AnonyMouse

Reputation: 18630

Moq mock method with out specifying input parameter

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?

Upvotes: 173

Views: 112311

Answers (3)

NDC
NDC

Reputation: 531

You can try the following:

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

Allows:

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

Upvotes: 14

Jeff Ogata
Jeff Ogata

Reputation: 57783

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.

Upvotes: 288

jeha
jeha

Reputation: 10700

Try using It.IsAny<FinancialYearLookup>() to accept any argument:

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

Upvotes: 20

Related Questions