我可以使用 Rhino Mocks 存根静态扩展方法,但是如果我将返回值转换为另一种类型,则会出现错误。为什么?
using Rhino.Mocks;
public interface INumberGenerator
{
double GetDouble();
}
static class NumberGeneratorExtensionMethods
{
public static double GetTheDouble(this INumberGenerator input)
{
return input.GetDouble();
}
public static decimal GetTheDoubleCastToDecimal(this INumberGenerator input)
{
return (decimal) input.GetDouble();
}
}
class MockExample
{
public void TriggerTheError()
{
var stub = MockRepository.GenerateStub<INumberGenerator>();
// This works
stub.Stub(obj => obj.GetTheDouble()).Return(1.2d);
// This throws the error
stub.Stub(obj => obj.GetTheDoubleCastToDecimal()).Return(1.2m);
}
}
这是错误:
System.InvalidOperationException:类型“System.Decimal”与方法“INumberGenerator.GetDouble();”的返回类型“System.Double”不匹配