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.

Advertisement

3 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.

Comments are closed.