FakeItEasy代理方法调用实际实现

我正在尝试将对虚假对象的调用代理到实际实现。 这样做的原因是我希望能够使用Machine.Specifications的WasToldTo和WhenToldTo,它仅适用于接口类型的伪造。

因此,我正在执行以下操作来代理对我的真实对象的所有调用。

public static TFake Proxy(TFake fake, TInstance instance) where TInstance : TFake { fake.Configure().AnyCall().Invokes(x => x.Method.Invoke(instance, x.Arguments.ToArray())); return fake; } 

我会像这样使用它。

 var fake = Proxy(A.Fake(), new SomeImplementation()); //in my assertions using Machine.Specifications (reason I need a fake of an interface) fake.WasToldTo(x => x.DoOperation()); 

然而问题是这只适用于void方法,因为Invokes方法没有对返回值做任何事情。 (Action param代替Func)

然后我尝试使用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

从我在FakeItEasy github存储库中的响应窃取:

你可以创建一个假的来包装现有的对象,如下所示:

 var wrapped = new FooClass("foo", "bar"); var foo = A.Fake(x => x.Wrapping(wrapped)); 

(示例来自创建假动作>显式创建选项 )

应该委托对底层对象的所有调用,通常需要注意任何重定向调用都必须是可重写的 。

我希望这有帮助。 如果没有,请再回来解释一下。 也许我会更好地理解它。

哦,请注意Configure机制。 它在FakeItEasy 2.0.0中消失了。 首选的习语是

 A.CallTo(fake).Invokes(…); // or A.CallTo(fake).WithReturnType(…);