Are multiple coexistent instances of MockRepository possible? If yes under which conditions?

0

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).

  • Analyzing the Hippomocks header shows that this use case doesn't seem to be foreseen (cf. MockRepoInstanceHolder). What is the reason behind allowing only one instance?
  • I can understand that for example different ExpectCallFunc in the MockRepository instances can contradict. Is it the reason behind it?
  • We could solve the problem in our example by moving the 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?
c++
unit-testing
hippomocks
asked on Stack Overflow Oct 10, 2014 by Roland Sarrazin

2 Answers

0

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.

answered on Stack Overflow Nov 19, 2014 by dascandy
0

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

answered on Stack Overflow Nov 19, 2014 by Richard.J • edited Nov 19, 2014 by Richard.J

User contributions licensed under CC BY-SA 3.0