开发者

Using callback triggers with RhinoMocks

开发者 https://www.devze.com 2022-12-14 14:28 出处:网络
I\'m writing unit tests using RhinoMocks for mocking, and now I\'m in need of some new functionality which I haven\'t used before.

I'm writing unit tests using RhinoMocks for mocking, and now I'm in need of some new functionality which I haven't used before.

I'd like to call a function, which again will call an async function. To simulate that the async function finishes and triggers the given callback with the result from execution I assume I can use the Callback functionality in RhinoMocks, but how do I do this?

Basically what I'd like to do is something like this:

fakeSomething = MockRepository.GenerateMock<ISomething>();
fakeSomething.FictionousFunctionSettingCallback(MyFunctionCalle开发者_高级运维d, MyCallback, theParameterToGiveCallback);
var myObj = new MyClass(fakeSomething);    
myObj.Foo(); 
// Foo() now fires the function MyFunctionCalled asynchronous, 
// and eventually would trigger some callback

So; is there a true function I can replace this "FictionousFunction" with to set this up? Please ask if this was unclear..


Just specify it using WhenCalled:

fakeSomething = MockRepository.GenerateMock<ISomething>();
fakeSomething
  .Stub(x => x.Foo())
  .WhenCalled(call => /* do whatever you want */);

for instance, you can use the Arguments property of the call argument:

fakeSomething
  .Stub(x => x.Foo(Arg<int>.Is.Anything))
  .WhenCalled(call => 
  { 
    int myArg = (int)call.Arguments[0]; // first arg is an int
    DoStuff(myArg);
  });

It is not asynchronous. You most probably don't need it to be asynchronous, it makes your life easier anyway if it isn't.

0

精彩评论

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