I am using Hippomocks and have a class that implements an generic interface. When I place expectations on this class I do not get the expected behaviour.
This is my minimum "working" example
template <class T>
struct Foo {
virtual ~Foo() = default;
virtual void bar(const T& t) = 0;
};
struct Baz : public Foo<int>, public Foo<double> {
void bar(const int& t) override = 0;
void bar(const double& t) override = 0;
};
TEST_CASE("Foo")
{
MockRepository mocks;
auto baz = mocks.Mock<Baz>();
mocks.ExpectCall(baz, Foo<int>::bar);
baz->bar(12);
mocks.ExpectCall(baz, Foo<double>::bar);
baz->bar(234.3);
}
I am expecting this test to pass without any problems. However the test fails with the following message (slightly edited to remove project names):
test_foo.cpp|28| FAILED:
|| due to unexpected exception with message:
|| Function called without expectation!
|| Expectations set:
|| test_foo.cpp(31)
|| : Expectation for Foo<int>::bar(...) on the mock at 0x0x5569df8f3400 was
|| satisfied.
|| test_foo.cpp(34)
|| : Expectation for Foo<double>::bar(...) on the mock at 0x0x5569df8f3400 was
|| not satisfied.
I am expecting the bar() belonging to Foo<double> to be invoked.
I was curious how
Mockcould be possibly implemented without manually defining all the methods (like other mocking libraries do), but it turns out that it does not work.Mockimplementation employs Undefined Behavior because it justreinterpret_castunrelated class toBaz:It does various other cheesy things, such as messing with vtable. None of that stuff can work reliably.