Moq Setup and ignore all arguments

I’m a happy fan of Moq. However, it’s sometimes annoying when the parameters do not matter, such as:

mock
    .Setup(x => x.Method(It.IsAny<Type>(), It.IsAny<AnotherType>(), It.IsAny<YetAnotherType>())
    .Return(value);

So I’ve made an extension method so that the parameters will be ignored, so you can write:

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

The code is available at Gist.

2 thoughts on “Moq Setup and ignore all arguments

  1. Great, thanks. BTW you missed a bracket at the end of your method SetupIgnoreArgs() in the example you gave and the method name is Returns() not Return() – ie you missed the ‘s’ at the end

  2. David Vesely says:

    It is great. Just had to remove the check at method VisitConstant of class MakeAnyVisitor to allow calls to methods with value type arguments.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s