FakeItEasy - 让接口假继承自抽象,同时共享相同的接口继承

FakeItEasy - Having an interface fake inherit from abstract while both share same interface inheritance

我有接口

public interface IInterface { void DoSomething(); }

另一个界面

public interface IOtherInterface : IInterface { }

一篇摘要class

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

我正在编写单元测试和伪造的 IOtherInterface。摘要 class 已经包含了我想用于我的单元测试的有用方法。我如何让我的 A.Fake<IOtherInterface>(); 继承自 AbstractClass

这是我迄今为止尝试过的方法,但它不起作用 - AbstractClass.DoSomething 没有被击中。

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

        fake.DoSomething();

当然,如果我做一个像这样的代理:

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

        fake.DoSomething();

...一切如我所愿。是否有内置机制来实现此目的,以便我不需要该代理 abstractFake 对象?

更新

我需要 IOtherInterface 因为我有一个客户端 class 需要 IOtherInterface 作为依赖:

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 仅用于接口,因此正确的使用方法是伪造一个接口或 class 并使用 Implements 添加额外的接口。我认为它应该向你抱怨,所以我提出了 complain when IFakeOptionsBuilder.Implements is passed a non-interface,它已在 FakeItEasy 2.0.0.

中修复

CallsBaseMethod 将确保执行摘要 class 的方法。

我会推荐 builder.CallsBaseMethods(),但这无法重定向呼叫。我认为这是因为它正在重定向 AbstractClass.DoSomething,但是当我们将假的投射到 IOtherInterface 并调用 DoSomething 时,它不匹配。我提出了 investigate interaction between Implements and CallsBaseMethods