开发者

RhinoMocks - Fetching parameters of called functions

Using RhinoMocks - can I fetch the parameters of a called function? I mean; can I get some of the unknown parameters from the function call out?

I have a mock, and I expect some function to be called on th开发者_如何转开发is. I know one of the parameters, but the other one is unknown as this comes from the class that uses the mock and calls a function on it. More specificly - in this case - the unknown argument is a lambda function. This is a callback function that is supposed to be called when the function is finished executing. As the mock prevents the callback from being called I want to fetch it and call it myself.

So; I want to check that the function was called. I want to make sure some of the arguments were the expected ones. And I want to get out the unknown arguments to do some operations on them afterwards.

Assuming both arguments are ints (for simplicity) I'd like to do something like this:

int unknownInt; 
_fakeSomething.AssertWasCalled(factory => factory.Foo(1, out unknownInt));
// then play around with unknownInt.. 

Can this be done? I see there is an Arg.Out, but couldn't quite make it work..

Note: Updated the question as it seemed to be misleading.


Arg<string>.Matches(arg => you got the argument here...);

UPDATE:

To fetch the second argument made on the first call of the Foo method on _fakeSomething:

string someArg = null;
var args = _fakeSomething.GetArgumentsForCallsMadeOn(
    x => x.Foo(0, 0), 
    x => x.IgnoreArguments()
);
var int = (int)args[0][1];


Not sure if it can be done, but testing like that can result in unreliable tests, as you don't know the actual paramter that was passed in.

If possible, test on explicit data. If you f.eks pass in null instead of a real value your test will likely pass for the wrong reason.

Testing with Arg.Is.Anything should be done carefully and when you truly don't care about the parameter, such as in AssertWasNotCalled.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜