Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rhino Mocks: How to stub a generic method to catch an anonymous type?

We need to stub a generic method which will be called using an anonymous type as the type parameter. Consider:

interface IProgressReporter
{
    T Report<T>(T progressUpdater);
}

// Unit test arrange:
Func<object, object> returnArg = (x => x);   // we wish to return the argument
_reporter.Stub(x => x.Report<object>(null).IgnoreArguments().Do(returnArg);

This would work if the actual call to .Report<T>() in the method under test was done with object as the type parameter, but in actuality, the method is called with T being an anonymous type. This type is not available outside of the method under test. As a result, the stub is never called.

Is it possible to stub a generic method without specifying the type parameter?

like image 757
Tor Haugen Avatar asked May 31 '11 10:05

Tor Haugen


1 Answers

I'm not clear on your use case but you might be able to use a helper method to setup the Stub for each test. I don't have RhinoMocks so have been unable to verify if this will work

private void HelperMethod<T>()
{
  Func<object, object> returnArg = (x => x); // or use T in place of object if thats what you require
  _reporter.Stub(x => x.Report<T>(null)).IgnoreArguments().Do(returnArg);
}

Then in your test do:

public void MyTest()
{
   HelperMethod<yourtype>();
   // rest of your test code
}
like image 70
IndigoDelta Avatar answered Nov 05 '22 09:11

IndigoDelta