相关疑难解决方法(0)

FakeItEasy代理方法调用实际实现

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

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

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;
}
Run Code Online (Sandbox Code Playgroud)

我会像这样使用它.

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());
Run Code Online (Sandbox Code Playgroud)

然而问题是这只适用于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;
}
Run Code Online (Sandbox Code Playgroud)

然而,Invokes方法仍然不能按我想要的方式工作(仍然是Action而不是Func).所以仍然没有使用返回值.

有没有办法用当前的最新版本实现这一目标?

我已经在FakeItEasy …

c# unit-testing mocking mspec fakeiteasy

7
推荐指数
1
解决办法
1966
查看次数

标签 统计

c# ×1

fakeiteasy ×1

mocking ×1

mspec ×1

unit-testing ×1