0

我在这种情况下嘲笑:

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};
}

所以我追求某种代理工厂,它通常支持一个接口并在多个接口上调用相同的方法。当然,当一个方法返回一个值时,每个方法都需要返回相同的值,或者可能需要先入为主。

我想在测试期间有效地窥探接口调用,但没有模拟是行尾。

如果有任何区别,我正在使用最小起订量。

4

1 回答 1

4

据我所知,没有办法对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.

于 2012-06-15T14:36:03.700 回答