2015-01-13 7 views
5

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

Antwort

4

Diebstahl von meinem response at the FakeItEasy github repository:

Sie eine gefälschte erstellen können, wie so ein bestehendes Objekt zu wickeln:

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

(Beispiel aus Creating Fakes > Explicit Creation Options genommen)

Das sollte Delegierten alle Aufrufe an das zugrunde liegende Objekt mit der üblichen Einschränkung, dass alle umgeleiteten Aufrufe overrideable sein müssen.

Ich hoffe, das hilft. Wenn nicht, komm zurück und erkläre es noch einmal. Vielleicht werde ich es besser verstehen.

Oh, und hüte dich vor dem Configure Mechanismus. Es geht weg in FakeItEasy 2.0.0. Das bevorzugte Idiom ist

A.CallTo(fake).Invokes(…); // or 
A.CallTo(fake).WithReturnType<bool>(…); 
+0

Danke, der Wrapping-Ansatz löst mein Problem. –