I am using googlemock to mock out an std::fstream object in my unit tests, like this:
TEST_F(SomeTest, SomethingIsDoneCorrectly)
{
class MockFstream : public std::fstream {};
MockFstream lMockFstream;
// Expectations and assertions here
}
When I compile I get the following warnings:
Warning 1 warning C4250: ‘SomeTest_SomethingIsDoneCorrectly_Test::TestBody::MockFstream’ : inherits ‘std::basic_istream<_Elem,_Traits>::std::basic_istream<_Elem,_Traits>::_Add_vtordisp1’ via dominance
Warning 2 warning C4250: ‘SomeTest_SomethingIsDoneCorrectly_Test::TestBody::MockFstream’ : inherits ‘std::basic_ostream<_Elem,_Traits>::std::basic_ostream<_Elem,_Traits>::_Add_vtordisp2’ via dominance
I’d prefer clean build output, so I want to suppress these specific warnings, but I’m writing cross-platform code, so I’d also prefer to avoid compiler-specific #pragmas.
Is there something I can do in the googlemock object that will hide these warnings?
It turns out that these warnings are a side-effect from some quirks in the Microsoft C++ STL. I’ve quoted an explanation from the relevant Microsoft Connect issue below.
My solution was simply to implement empty versions of the inherited functions in my mock:
An explanation from Microsoft about why the warning occurs: