Moq - Setup Property to return string from method parameter
I'm trying to do the f开发者_StackOverflowollowing:
mockObject.Setup( a => a.MyObject.MyMethod( It.IsAny<string>() ).MyProperty ).Returns( ?? );
where the Returns() returns whatever string is input to MyMethod.
Is this possible?
When I try the following, I get System.Reflection.TargetParameterCountException: Parameter count mismatch.
mockObject.Setup( a => a.MyObject.MyMethod( It.IsAny<string>() ).MyProperty ).Returns( (string s) => s );
How about something like this:
mockObject.Setup( a => a.MyObject.MyMethod( It.IsAny<string>() ) )
.Returns( (string s) =>
{
var mockReturnedObject = new Mock<Returned>();
mockReturnedObject.Setup(o => o.MyProperty).Returns(s);
return mockReturnedObject.Object;
} );
Or, if your "returned object" is just a POCO:
mockObject.Setup( a => a.MyObject.MyMethod( It.IsAny<string>() ) )
.Returns( (string s) => new Returned {MyProperty = s} );
精彩评论