Ich versuche, Aufrufe an ein gefälschtes Objekt zu der tatsächlichen Implementierung Proxy. Der Grund dafür ist, dass ich das WasToldTo und das WhenToldTo von Machine.Specifications verwenden kann, das nur auf Fälschungen eines Schnittstellentyps funktioniert.FakeItEasy Proxy-Methoden Aufrufe an echte Implementierung
Daher mache ich Folgendes, um alle Aufrufe an mein reales Objekt Proxy.
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;
}
Ich würde das so verwenden. jedoch
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());
Das Problem ist, dass dies funktioniert nur für nichtig Methoden, da die Invokes Methode nichts mit dem Rückgabewert zu tun. (Aktionsparameter anstelle von Func)
Dann habe ich versucht, dies mit der WithReturnValue-Methode zu tun.
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;
}
Allerdings funktioniert die Methode Invokes immer noch nicht so, wie ich es will (immer noch Action statt Func). So wird der Rückgabewert immer noch nicht verwendet.
Gibt es eine Möglichkeit, dies mit der aktuellsten Version zu erreichen?
Ich habe bereits ein Problem bei der FakeItEasy Github-Repository eingereicht. https://github.com/FakeItEasy/FakeItEasy/issues/435
Danke, der Wrapping-Ansatz löst mein Problem. –