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

醉酒当歌 提交于 2019-12-05 00:45:52
IndigoDelta

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
}

To answer your question: no, it is not possible to stub a generic method without knowing the generic arguments with Rhino. The generic arguments are an essential part of the method signature in Rhino, and there is no "Any".

The easiest way in your case would be to just write a hand written mock class, which provides a dummy implementation of IProgressReporter.

class ProgressReporterMock : IProgressReporter
{
    T Report<T>(T progressUpdater)
    {
      return progressUpdater;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!