我正在尝试将对假对象的调用代理到实际的实现。这样做的原因是我希望能够使用 Machine.Specifications 的 WasToldTo 和 WhenToldTo ,它们仅适用于接口类型的伪造。
因此,我正在执行以下操作来代理对我的真实对象的所有调用。
public static TFake Proxy<TFake, TInstance>(TFake fake, TInstance instance) where TInstance : TFake
{
fake.Configure().AnyCall().Invokes(x => x.Method.Invoke(instance, x.Arguments.ToArray()));
return fake;
}
我会像这样使用它。
var fake = Proxy<ISomeInterface, SomeImplementation>(A.Fake<ISomeInterface>(), new SomeImplementation());
//in my assertions using Machine.Specifications (reason I need a fake of an interface)
fake.WasToldTo(x => x.DoOperation());
但问题是这仅适用于 void 方法,因为 Invokes 方法不对返回值执行任何操作。 (动作参数而不是功能)
然后我尝试使用 WithReturnValue 方法来执行此操作。
public static TFake Proxy(TFake fake, TInstance instance) where TInstance : TFake
{
fake.Configure().AnyCall()..WithReturnType().Invokes(x => x.Method.Invoke(instance, x.Arguments.ToArray()));
fake.Configure().AnyCall()..WithReturnType().Invokes(x => x.Method.Invoke(instance, x.Arguments.ToArray()));
fake.Configure().AnyCall()..WithReturnType().Invokes(x => x.Method.Invoke(instance, x.Arguments.ToArray()));
//etc.
return fake;
}
但是 Invokes 方法仍然无法按照我想要的方式工作(仍然是 Action 而不是 Func)。所以返回值还是没有使用。
有没有办法用当前的最新版本来实现这一点?
我已经在 FakeItEasy github 存储库中提交了一个问题。https://github.com/FakeItEasy/FakeItEasy/issues/435 https://github.com/FakeItEasy/FakeItEasy/issues/435