我正在处理一个棕色地带应用程序。由于高复杂性并且没有现有测试,因此依赖注入不是一种选择。此代码也不编程到接口。因此,我提出的任何解决方案都不得使用DI或接口。
我正在考虑使用fakeiteasy来查看它是否可以为我们提供一些不需要重大重构工作的测试功能。如果我能设法获得许可证,我也在考虑微软假货。
具体来说,我想知道是否可以覆盖基类实现,同时还测试派生类:
[TestMethod]
public void Test()
{
//I want SomeBase to be fully mocked.
var baseFake = A.Fake<SomeBase>();
//Class under test. Create wrapper - unconfigured calls will be forwarded to wrapped
SomeDerived someDerived = new SomeDerived();
var derivedFakeWrapper = A.Fake<SomeDerived>(x => x.Wrapping(someDerived));
A.CallTo(() => derivedFakeWrapper.OtherCall()).DoesNothing();
//A.CallTo(() => derivedFakeWrapper.SomeCall()).DoesNothing();//This works. But I want to test SomeCall()
A.CallTo((() => derivedFakeWrapper.DoWork())).DoesNothing();//SomeCall makes a call to DoWork(). DoWork() should do nothing
A.CallTo((() => baseFake.DoWork())).DoesNothing();//Tried sticking somethin in the base
derivedFakeWrapper.SomeCall();
derivedFakeWrapper.OtherCall();
}
public class SomeBase
{
public virtual void DoWork()
{
Console.WriteLine("base implementation");
}
}
public class SomeDerived : SomeBase
{
public virtual void SomeCall()
{
Console.WriteLine("Do other work");
DoWork();
}
public virtual void OtherCall()
{
Console.WriteLine("Other call working normally");
}
}
答案 0 :(得分:1)
我不会在这里介绍一个包装器。我认为这只会增加复杂性,没有任何好处。 (当然,我根本不想这样测试,但考虑到你已经制定的限制,我同意:你可以做的事情不多。)
相反,我会使用指令伪造正在测试的类,以将所有调用转发到原始实现。然后配置DoWork
不执行任何操作:
[Test]
public void Test()
{
// Class under test. Unconfigured calls will use actual implementation
var fakeDerived = A.Fake<SomeDerived>(x => x.CallsBaseMethods());
A.CallTo(() => fakeDerived.DoWork()).DoesNothing();
A.CallTo(() => fakeDerived.OtherCall()).DoesNothing();
fakeDerived.SomeCall();
fakeDerived.OtherCall();
}