开发者

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜