FakeItEasy – 有一个接口伪inheritance自abstract,而两者共享相同的接口inheritance
我有一个界面
public interface IInterface { void DoSomething(); }
另一个界面
public interface IOtherInterface : IInterface { }
一个抽象类
public abstract class AbstractClass : IInterface { public void DoSomething() { Console.WriteLine("Got here"); } }
我正在编写unit testing并伪造IOtherInterface。 抽象类已经包含了我想要用于unit testing的有用方法。 我如何制作A.Fake();
inheritance自AbstractClass
?
这是我到目前为止尝试但它不起作用 – AbstractClass.DoSomething没有被击中。
IOtherInterface fake = A.Fake(builder => builder.Implements(typeof (AbstractClass))); fake.DoSomething();
当然,如果我做一个像这样的代理:
var abstractFake = A.Fake(); A.CallTo(() => fake.DoSomething()).Invokes(abstractFake.DoSomething); fake.DoSomething();
……事情按我的意愿运作。 是否有内置机制来实现这一点,以便我不需要代理abstractFake
对象?
UPDATE
我需要IOtherInterface
因为我有一个客户端类需要IOtherInterface作为依赖:
class Consumer { public Consumer(IOtherInterface otherInterface) { otherInterface.DoSomething(); } }
var fake = (IOtherInterface) A.Fake(builder => builder.Implements(typeof(IOtherInterface))); A.CallTo(() => fake.DoSomething()).CallsBaseMethod(); fake.DoSomething();
Implements
仅适用于接口,因此使用它的正确方法是伪造接口或类并使用Implements
添加其他接口。 我认为它应该向你抱怨,所以当IFakeOptionsBuilder.Implements传递非接口时我提出了抱怨 , 这在FakeItEasy 2.0.0中得到了修复 。
CallsBaseMethod
将确保执行Abstract类的方法。
我会推荐builder.CallsBaseMethods()
,但这无法重定向调用。 我认为这是因为它重定向AbstractClass.DoSomething
,但是当我们将假冒伪劣转换为IOtherInterface
并调用DoSomething
,它不匹配。 我已经提出调查Implements
和CallsBaseMethods
之间的交互 。