Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using callback triggers with RhinoMocks

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(MyFunctionCalled, 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..

like image 672
stiank81 Avatar asked Dec 23 '09 14:12

stiank81


1 Answers

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.

like image 147
Stefan Steinegger Avatar answered Oct 02 '22 05:10

Stefan Steinegger