Returning value that was passed into a method

Steve Dunn picture Steve Dunn · Jun 15, 2009 · Viewed 134.4k times · Source

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?

Answer

mhamrah picture mhamrah · Jun 15, 2009

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

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

Or slightly more readable:

.Returns<string>(x => x);