我们需要 stub 一个通用方法,该方法将使用匿名类型作为类型参数来调用。考虑:
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);
如果在被测方法中对 .Report
是否可以在不指定类型参数的情况下对泛型方法进行 stub ?
最佳答案
我不清楚您的用例,但您可以使用辅助方法为每个测试设置 stub 。我没有 RhinoMocks,所以无法验证这是否有效
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);
}
然后在你的测试中做:
public void MyTest()
{
HelperMethod<yourtype>();
// rest of your test code
}
关于c# - 犀牛模拟 : How to stub a generic method to catch an anonymous type?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6186412/