I'm trying to proxy calls to a fake object to the actual implementation. The reason for this is that I want to be able to use the WasToldTo and WhenToldTo of Machine.Specifications which only works on fakes of an interface type.
Therefore I'm doing the following to proxy all calls to my real object.
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;
}
I would use that like this.
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());
The problem however is that this only works for void methods, since the Invokes method is not doing anything with the return value. (Action param instead of Func)
Then I was trying to do this using the WithReturnValue method.
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;
}
However the Invokes method still doesn't work the way I want it (still Action instead of Func). So The return value is still not used.
Is there a way of achieving this with the current latest version?
I already filed an issue at the FakeItEasy github repository. https://github.com/FakeItEasy/FakeItEasy/issues/435