Can Hippomocks set expectations for the same MockRepository in different source files?

313 Views Asked by At

I would like to have multiple tests in multiple files be able to call a separately-compiled utility method from another file that sets up the same expectations for each test. However, Hippomocks seems to have a problem with setting expectations for the same MockRepository in different source files.

Here's a simple example:

file1.cpp is:

void SetOtherExpectations(MockRepository &mocks);

int Method45()
{
    return -45; 
}
int Method88()
{
    return -88;
}

TEST(TestOfSettingValueInAnotherSourceFile)
{
    MockRepository mocks;
    mocks.OnCallFunc(Method45).Return(45);
    mocks.OnCallFunc(Method88).Return(88);
    SetOtherExpectations(mocks);
    testEqual(45, Method45()); // Failed Condition: (Expected: <45>, actual: <8888>
}

file2.cpp is:

int MethodInt(int)
{
    return -145;
}
int MethodChar(char)
{
    return -188;
}

void SetOtherExpectations(testFramework::MockRepository &mocks)
{
    mocks.OnCallFunc(MethodChar).Return(8888); // Line A
    mocks.OnCallFunc(MethodInt).Return(9999); // Line B
}

If I swap line A and B, the Method45 call now returns 9999. It turns out that when the Method45 call is made, the search for the matching expectation finds the first mocked function in file2 before it finds the correct mocked function from file1.

There are 4 mocked methods in the MockRepository, but Hippomocks assigns a funcIndex value on a per-source-file basis, since it uses the __COUNTER__ preprocessor variable (which starts at 0 in each source file and is incremented by 1 every time it is used in that source file) in the calls to RegisterExpect. Therefore each subsequent expectation setting in a separate source file “hides” all previous expectations set with that index.

It seems like I need to include as inline code all the utility functions that set expectations into each separately-compiled source file, which is not a great solution. Is there any other way to do this?

0

There are 0 best solutions below