I want to mock myFunction in google test and am having issues with the two templates.
template <class Outer>
class MyClass{
template <class T>
void myFunction(const int a, T * b);
};
First of all, Outer
template type isn't an issue here since it is not used in myFunction
signature.
To handle type T
you will need to fully specialize mocked method for all types used during testing.
Imagine you want to test that method with T=std::string
:
template <class Outer>
class MyClassMock {
public:
MOCK_METHOD(void, myFunction, (const int, std::string*));
template <class T>
void myFunction(const int a, T* b);
template <>
void myFunction(const int a, std::string* b)
{
myFunction(a, b);
}
};
If your tested function will have signature like this:
template <typename TMyClass>
void UseMyClassWithString(TMyClass& i_value)
{
std::string t;
i_value.myFunction(5, &t);
}
Result test may looks like this:
TEST(UseMyClass, ShouldCallMyFunction)
{
MyClassMock<size_t> mock;
EXPECT_CALL(mock, myFunction).Times(1);
UseMyClassWithString(mock);
}
Here your Outer
type is size_t
and it is used only to create mock object.