563

I have a method on an interface:

string DoSomething(string whatever);

I want to mock this with MOQ, so that it returns whatever was passed in - something like:

_mock.Setup( theObject => theObject.DoSomething( It.IsAny<string>( ) ) )
   .Returns( [the parameter that was passed] ) ;

Any ideas?

5 Answers 5

762

You can use a lambda with an input parameter, like so:

.Returns((string myval) => { return myval; });

Or slightly more readable:

.Returns<string>(x => x);
8
  • 1
    Seems quite easy until you need to do this for a method with 7 arguments... When I inspected IReturns in Moq, it defines Returns for 4 arguments at most. Any easy way to get around that? /I mean except modifying Moq source/ Commented Jun 26, 2013 at 16:27
  • 18
    ok, it's defined for up to 9 arguments in Moq v 4.0.0.0. solved :) Commented Jun 27, 2013 at 14:59
  • 22
    @mizukinakeshu I'd consider a bit of a refactor on a 9 argument method as it sounds like the class/method is doing too much. Perhaps refactor the 9 parameters into a settings class or struct to help you later? Commented Feb 8, 2015 at 21:36
  • 1
    @TheSenator Agree, I don't reacall already what this was about but I guess I was just hacking some unit tests together for already existing code I was not to modify, otherwise this number of arguments definitely calls for refactoring. Commented Feb 9, 2015 at 13:21
  • 53
    Just a note as this confused me: The string in .Returns<string> refers to the input parameter(s) and not the values you are returning.
    – Jim
    Commented Jul 31, 2015 at 15:13
398

Even more useful, if you have multiple parameters you can access any/all of them with:

_mock.Setup(x => x.DoSomething(It.IsAny<string>(),It.IsAny<string>(),It.IsAny<string>()))
     .Returns((string a, string b, string c) => string.Concat(a,b,c));

You always need to reference all the arguments, to match the method's signature, even if you're only going to use one of them.

8
  • 24
    This should be the accepted answer. This is exactly what you need to do. Anything else throws a "number of expected arguments" exception. Commented Feb 18, 2015 at 17:28
  • 2
    This answer saved the day. Note (future readers), you can take it a bit further as well. .Returns((string a, string b, string c) => { string d = "wow"; return string.Concat(a,b,c,d); } ); Commented Mar 3, 2017 at 19:53
  • 1
    Personally, this is a much better answer. I have very little knowledge of Moq but still I understood it immediately. Commented May 5, 2017 at 5:45
  • 2
    For methods returning void I used .Callback((string a, Exception b, string c) => throw new Exception(b.Message));
    – tmaj
    Commented Jun 12, 2018 at 6:51
  • 1
    I think you are missing one closing bracket
    – Anton Krug
    Commented Oct 6, 2023 at 22:54
97

The generic Returns<T> method can handle this situation nicely.

_mock.Setup(x => x.DoSomething(It.IsAny<string>())).Returns<string>(x => x);

Or if the method requires multiple inputs, specify them like so:

_mock.Setup(x => x.DoSomething(It.IsAny<string>(), It.IsAny<int>())).Returns((string x, int y) => x);
2

For those who are not using Moq, there's a way with NSubstitute.

mock.Method(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>()).Returns(callInfo =>
    {
         var arg1 = callInfo[0].ToString();
         var arg2 = callInfo[1].ToString();
         var argN = callInfo[n-1].ToString();
         return arg1 + arg2 + argN; //or whatever
    });
2

For those of you trying to use MOQ with Async methods:

repository.Setup(x => 
    x.SaveAsync(It.IsAny<List<Deposit>>(),
                It.IsAny<User>(), 
                null))
          .ReturnsAsync(
    (List<Deposit> x, User y, DateTime? z) => x);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.