Shilpa
Shilpa

Reputation: 19

Mocking simple c++ method fails on using googletest

I have a problem with mocking test ,below is my code :

struct Interface
{  
virtual ~Interface() {};
virtual struct group *read() {
  return ::read();
 } 
};


class MockSocket: public Socket::Interface
{
public:
    MOCK_METHOD0(read, struct group *());
};

TEST_F(ConfiguredGrent, ReceivedcorrectStructure) 
{
   StrictMock<MockSocket> ms;
   struct group value;

    EXPECT_CALL(ms, read()).Times(1).
    WillOnce(Return(&value)).
    RetiresOnSaturation();
}

while executing the above code , it fails with the below error :

/gmock-1.7.0/fused-src/gmock/gmock.h:11572:16: error: no matching function for call to 'MockSocket::MockSocket()'
   StrictMock() {

Please can any one suggest , where i am going wrong

Upvotes: 0

Views: 87

Answers (1)

Martin G
Martin G

Reputation: 18139

Your example is not the same as the code producing the error. You have managed to remove the interesting part when creating a minimal example for us.

Here is what can produce your error:

struct group{
};

struct Interface
{
  virtual ~Interface() {};
  virtual struct group *read() {
    return nullptr; // I added some dummy implementation instead of calling ::read but kept the interface non-pure virtual to keep it relevant
  }
};

class MockSocket: public Interface
{
public:
  MockSocket(int x){ (void) x; }
  MOCK_METHOD0(read, struct group *());
};

TEST(ConfiguredGrent, ReceivedcorrectStructure)
{
  StrictMock<MockSocket> ms;
  struct group value;

  EXPECT_CALL(ms, read())
    .Times(1)
    .WillOnce(Return(&value))
    .RetiresOnSaturation();
}

I have removed the default constructor by adding a different constructor, and here is the produced error:

error: no matching function for call to 'MockSocket::MockSocket()'
   StrictMock() {
                ^
note: candidate: MockSocket::MockSocket(int)
   MockSocket(int x){ (void) x; }
   ^

Because

StrictMock<MockSocket> ms; will call the default constructor identity.

Upvotes: 2

Related Questions