Using Visual Studio 2010 C++ with GMock. Trying to create a stub object for a third party class that is used by my classes but I'm getting the following error:
Error: object of abstract class type "ThirdPartyClassFake " is not allowed.
The third party class is defined like:
namespace ThirdPartyNamespace
{
class __declspec(novtable) ThirdPartyClass : public ThirdPartyBaseClass
{
public:
virtual bool Hello() const = 0;
virtual bool Goodbye() const = 0;
};
}
I created a mock of this:
namespace ThirdPartyNamespace {
class ThirdPartyClassFake : public ThirdPartyClass {
public:
MOCK_CONST_METHOD0(Hello, bool());
MOCK_CONST_METHOD0(Goodbye, bool());
};
}
Now in my test I'm trying to do:
TEST(MyService, WhenCalled_DoesTheRightThingTM) {
// Arrange
ThirdPartyClassFake stub;
// Act
...
// Assert
...
}
The error is on the "ThirdPartyClassFake stub;" line. Why am I getting this error and how can I successfully create a mock/stub object?
Specifically the problem was that although I implemented ThirdPartyClass
's virtual methods in my mock object, I neglected to implement ThirdPartyBaseClass
's virtual methods. This was causing the error. Once I added MOCK_METHOD
calls for those methods the error went away.
Class ThirdPartyClass
is an abstract class ( two pure virtual member functions ). Any class that derives from it must override/implement the virtual methods.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With