HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpCritical

Returning value that was passed into a method

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
returningmethodvaluethatpassedintowas

Problem

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( ) ) )
   .Returns( [the parameter that was passed] ) ;


Any ideas?

Solution

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

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


Or slightly more readable:

.Returns(x => x);

Code Snippets

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

Context

Stack Overflow Q#996602, score: 777

Revisions (0)

No revisions yet.