Is there a mocking framework that exists for C# that supports .Net 4.0 and C# completely. Specifically, I'm looking for it to support optional parameters.
Asked
Active
Viewed 3,960 times
8
-
I was actually able to find [NSubstitute](http://nsubstitute.github.com/) and it did what I need. – Mark S. Jul 12 '11 at 19:57
-
You should post that as an answer to your question and accept it. :) – Brian Dishaw Jul 12 '11 at 20:17
-
@Brian Dishaw - Thanks for the suggestion! – Mark S. Jul 12 '11 at 21:38
3 Answers
3
I was able to get Moq to handle this by writing an extension method that accepted a method name and a dictionary of the parameter names and values that you did want to specify and the extension method creates calls to It.IsAny() for all the parameters that you didn't specify:
http://d4nt.com/handling-method-parameter-default-values-using-moq/
d4nt
- 14,827
- 8
- 39
- 51
2
I'm pretty sure MOQ does - http://code.google.com/p/moq/
http://code.google.com/p/moq/issues/detail?id=221 - optional param support in .NET 4.0
slandau
- 22,816
- 40
- 119
- 181
-
2I tried that, but I get an `System.Reflection.TargetParameterCountException: Parameter count mismatch.` exception, when the method is called without specifying the specific parameter. – Mark S. Jul 12 '11 at 19:14
-
Hm...I'm not sure then. I've never actually done this, I just thought it was supported. – slandau Jul 12 '11 at 19:16
-
8It works for me, I just had to make sure my .Returns line had the correct definition also (INCLUDE the optional parameter, or no parameters). So `.Returns(() => someReturnValue);` or `.Returns((int requiredVar, bool optionalVar) => someReturnValue);`. And the .Setup needs them both as well obviously. – CaffGeek Jun 08 '12 at 14:26
-
If you get TargetParameterCountException, check your Returns has the correct number of parameters – Chris Haines Nov 02 '12 at 16:28
-
@MarkS., have a look on this answer: http://stackoverflow.com/a/9704789/79485 As CaffGeek and Hainesy said, it's about the .Returns parameter. – Marcel Dec 03 '13 at 12:35
1
I was actually able to find NSubstitute and it did what I need.
Mark S.
- 1,496
- 1
- 16
- 26
-
-
Can't mock methods that require multi-dimensional arrays either http://stackoverflow.com/questions/9912368/two-dimensional-object-array-return-type-nsubstitute – Myles McDonnell Mar 29 '12 at 09:48