3

I'm trying to do the following:

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 );

1 Answer 1

2

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} );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.