接口拆分器
本文关键字:拆分 接口 | 更新日期: 2023-09-27 18:37:14
在这种情况下,我在嘲笑:
class A
{
public IB B {get; set;}
}
interface IB
{
//methods
}
class B : IB
{
//IB methods
}
public SimpleMockTest()
{
var mockIB = new Mock<IB>();
var a = new A{B=mockIB.Object};
//do stuff with A, then verify methods on IB were called
}
public TheKindOfTestIWantToDo()
{
var actualB = new B();
var mockIB = new Mock<IB>();
var splitter = new Splitter<IB>(actualB, mockIB.Object); //I need something like this
var a = new A{B=splitter};
//do stuff with A, methods invoked on splitter IB get passed to both supplied instances
//of IB (actualB, and the mockIB). Allowing me to verify the calls as well as have the methods invoked on the actual B object.
//OR:
var mockIB2 = new Mock<IB>(actualB); //behaviour is dictated by the actual, but allows verification
var a2 = new A{B=mockIB2.Object};
}
所以我追求某种代理工厂,它通常支持一个接口并在多个接口上调用相同的方法。当然,如果方法返回一个值,则每个方法都需要返回相同的值,或者可能有一个方法优先返回。
我想在测试期间有效地窥探接口调用,但模拟不是终点。
如果有任何区别,我正在使用最小起订量。
据
我所知,没有办法对actualB
上的所有方法执行此操作。
我想你想要这样的东西:
public TheKindOfTestIWantToDo()
{
var actualB = new B();
var mockIB = new Mock<IB>();
mockIB.Setup(b => b.Foo()).Returns(() => actualB.Foo())
var a = new A { B = mockIB };
//do stuff with A, methods invoked on splitter IB get passed to both supplied instances
//of IB (actualB, and the mockIB). Allowing me to verify the calls as well as have the methods invoked on the actual B object.
}
不过,您可能应该重新考虑您的单元测试策略。 A
的行为应该是可测试的,而不依赖于B
的行为。 尝试让mockIB
返回特定值,然后为 B
编写一组不同的测试。