FakeItEasy代理方法调用真正执行 [英] FakeItEasy Proxy methods calls to real implementation

查看:197
本文介绍了FakeItEasy代理方法调用真正执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想代理调用一个假的对象的实际执行。这样做的原因是,我希望能够使用Machine.Specifications只适用于一个接口类型的假货WasToldTo和WhenToldTo。

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());



但问题是,这只是作品void的方法,因为调用方法是没有做任何与返回值。 (操作参数,而不是Func键)

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)

然后我尝试这种使用WithReturnValue方法来做。

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;
}



然而,调用方法仍然不能工作,我想要的方式(仍操作而不是Func键)。所以仍不能使用的返回值。

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.

是否与目前的最新版本

我已经在GitHub上FakeItEasy提起库的问题。 https://github.com/FakeItEasy/FakeItEasy/issues/435

I already filed an issue at the FakeItEasy github repository. https://github.com/FakeItEasy/FakeItEasy/issues/435

推荐答案

从我的响应在FakeItEasy GitHub的库

您可以创建一个假的来包装现有的对象,像这样:

You can create a fake to wrap an existing object like so:

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



(从的创建假货>明确创建选项

这的的委托所有调用底层对象,与通常的警告,任何转接呼叫必须的覆写投放

That should delegate all calls to the underlying object, with the usual caveat that any redirected calls have to be overrideable.

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

I hope this helps. If not, come back and explain again. Maybe I'll understand it better.

哦,当心配置机制。这是怎么回事客场FakeItEasy 2.0.0。
的首选成语是

Oh, and beware the Configure mechanism. It's going away in FakeItEasy 2.0.0. The preferred idiom is

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

这篇关于FakeItEasy代理方法调用真正执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆