Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Mock: object of abstract class type "xyz" is not allowed?

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?

like image 829
User Avatar asked Feb 24 '23 17:02

User


2 Answers

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.

like image 120
User Avatar answered Feb 26 '23 05:02

User


Class ThirdPartyClass is an abstract class ( two pure virtual member functions ). Any class that derives from it must override/implement the virtual methods.

like image 25
Mahesh Avatar answered Feb 26 '23 07:02

Mahesh