I am mocking in this sort of situation:
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};
}
So I am after some sort of proxy factory that would genericly support an interface and call the same method on multiple interfaces. Of course where a method returns a value, each would need to return the same value, or maybe one takes precident.
I want to effectively snoop on interface calls during the test, but without the mock being the end of the line.
Am using Moq if it makes any difference.
As far as I know, there isn’t a way to do this for all methods on
actualB.I think you want something like:
You should probably rethink your unit testing strategy, though. The behavior of
Ashould be testable without relying on the behavior ofB. Try havingmockIBreturn specific values, and then write a different set of tests forB.