FakeItEasy-伪造的接口继承自抽象,而两者共享相同的接口继承 [英] FakeItEasy - Having an interface fake inherit from abstract while both share same interface inheritance

查看:142
本文介绍了FakeItEasy-伪造的接口继承自抽象,而两者共享相同的接口继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个界面

public interface IInterface { void DoSomething(); }

另一个界面

public interface IOtherInterface : IInterface { }

抽象类

public abstract class AbstractClass : IInterface
{
    public void DoSomething()
    {
        Console.WriteLine("Got here");
    }
}

我正在编写单元测试和伪造的IOtherInterface.抽象类已经包含了一些我想在单元测试中使用的有用方法.我如何使我的A.Fake<IOtherInterface>();AbstractClass继承?

I'm writing a unit test and fake IOtherInterface. The abstract class already contains helpful methods I'd want to leverage for my unit test. How would I make my A.Fake<IOtherInterface>(); inherit from AbstractClass?

这是我到目前为止尝试过的方法,但是它不起作用-AbstractClass.DoSomething不会受到攻击.

This is what I've tried so far but it doesn't work - AbstractClass.DoSomething does not get hit.

        IOtherInterface fake = A.Fake<IOtherInterface>(builder => builder.Implements(typeof (AbstractClass)));

        fake.DoSomething();

当然,如果我做一个代理,例如:

Of course if I make a proxy like:

        var abstractFake = A.Fake<AbstractClass>();
        A.CallTo(() => fake.DoSomething()).Invokes(abstractFake.DoSomething);

        fake.DoSomething();

...事情按我的意愿工作.是否有内置的机制可以实现此目的,所以我不需要该代理abstractFake对象?

... things work as I wanted. Is there a built in mechanism to achieve this so that I don't need that proxy abstractFake object?

更新

我需要IOtherInterface,因为我有一个需要将该IOtherInterface作为依赖项的客户端类:

I need IOtherInterface because I have a client class that needs that IOtherInterface as dependency:

class Consumer
{
    public Consumer(IOtherInterface otherInterface)
    {
        otherInterface.DoSomething();
    }
}

推荐答案

var fake = (IOtherInterface) A.Fake<AbstractClass>(builder =>
                               builder.Implements(typeof(IOtherInterface)));
A.CallTo(() => fake.DoSomething()).CallsBaseMethod();
fake.DoSomething();

Implements仅用于接口,因此使用它的正确方法是伪造接口或类,并使用Implements添加其他接口.我认为应该向您抱怨,所以当IFakeOptionsBuilder.Implements通过非界面,(已在FakeItEasy 2.0.0中修复).

Implements is intended to work only with interfaces, so the proper way to use it is to fake an interface or class and use Implements to add additional interface. I think it should've complained at you, so I raised complain when IFakeOptionsBuilder.Implements is passed a non-interface, which was fixed in FakeItEasy 2.0.0.

CallsBaseMethod将确保执行Abstract类的方法.

The CallsBaseMethod will ensure that the Abstract class's method is executed.

我会推荐builder.CallsBaseMethods(),但这无法重定向呼叫.我认为这是因为它正在重定向AbstractClass.DoSomething,但是当我们将伪造的内容投射到IOtherInterface并调用DoSomething时,它就不匹配了.我提出了研究ImplementsCallsBaseMethods 之间的交互.

I would've recommended builder.CallsBaseMethods(), but this fails to redirect the call. I think it's because it's redirecting AbstractClass.DoSomething, but when we cast the fake to IOtherInterface and call DoSomething, it's not matching. I've raised investigate interaction between Implements and CallsBaseMethods.

这篇关于FakeItEasy-伪造的接口继承自抽象,而两者共享相同的接口继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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