Hippomocks' MockRepository
supports multiple instances as far as the compiler is concerned. Some use cases are not possible, though, as they lead to unit test executable crash. Let's consider the following example.
void MyCall()
{
}
void MyCall2()
{
}
void MySubTest()
{
MockRepository mockRep2;
mockRep2.ExpectCallFunc(MyCall2);
MyCall2();
}
void MyTest()
{
MockRepository mockRep1;
mockRep1.ExpectCallFunc(MyCall);
MySubTest();
MyCall();
}
This example leads (in Visual C++ 2010) to an unhandled exception (access violation reading location 0x00000048).
MockRepoInstanceHolder
). What is the reason behind allowing only one instance?ExpectCallFunc
in the MockRepository
instances can contradict. Is it the reason behind it?mockRep1
instantiation to after the MySubTest()
call, but we had a hard time understanding the problem in a first place. Is there a compile-time or run-time possibility to identify such multiple instances explicitly?The root problem is that the (compile-time generated) function does not have a place to 'tag' it with a certain mock repository and unlike mock objects it cannot be grown to carry it as well. That's what the holder is for - to find the mock repository from the generated stub function.
It sounds like a solvable problem if there's a good use case for it, by making the generated code in that function a bit more complex. I must admit that I don't see a direct immediate advantage, especially as mocking the same function twice in two separate repos wouldn't work anyway (because you're hard-overwriting the same function) and it would lead to very difficult to debug problems if you mix them up at some point. I don't think the added complexity and debugging difficulties weigh up against the feature added by this.
I came across the same problem. Using cppunit as test framework, I made the MockRepository a member of the test frame class. If there are several test cases, cppunit instantiates a new object of the test frame class for each test case. This overwrites the MockRepositoryHolder. I got the exception when trying to mock an Windows API function in the first test case, but HippoMocks referenced the last written MockRepository, which if the one of the last test case. For me, a workaround is to either make the MockRepository local to each test case or add a new, local MockRepository only for mocking static functions
User contributions licensed under CC BY-SA 3.0