Derived
class and passes this object as a pointer to a function accepting a pointer of the base/parent Base
class.Now I wanted to test this using the Google Test Framework in conjunction with Google Mock:
TEST_F(myClassToBeTestedTest, functionToBeTestedTest)
{
EXPECT_CALL(myClassMock, myFunc(::testing::A<Derived *>()));
myClassToBeTested.functionToBeTested();
}
This did not compile with the following error:
mytestclasstest.cpp:X:Y: error: no matching function for call to 'MyClassMock::gmock_myFunc(testing::Matcher<Derived*>)'
mytestclasstest.cpp:X:Y: note: candidate is:
myclassmock.h:X:Y: note: testing::internal::MockSpec<void(Base*)>& 'MyClassMock::gmock_myFunc(const testing::Matcher<Base*>&)
...
Googling a bit I found http://stackoverflow.com/questions/5056538/google-mock-using-testingan which explained that Google Test needed
const
and &
. However I couldn't get this to make sense for my type argument. E.g. EXPECT_CALL(myClassMock, myFunc(const ::testing::A<Derived *>() &));
doesn't work.After more searching (and annoyance at google for removing :: and <>) I ended up with creating my own custom matcher using "Using Predicates as Matchers" from http://code.google.com/p/googlemock/wiki/CookBook#Using_Predicates_as_Matchers together with http://stackoverflow.com/questions/500493/c-equivalent-of-instanceof to check if object is type of Derived:
int IsDerived(Base * base)
{
Derived * derived = dynamic_cast<Derived *>(base);
return derived != NULL ? 1 : 0;
}
TEST_F(myClassToBeTestedTest, functionToBeTestedTest)
{
EXPECT_CALL(myClassMock, myFunc(::testing::Truly(IsDerived)));
myClassToBeTested.functionToBeTested();
}
No comments:
Post a Comment