2022-10-30

GMock EXPECT_CALL returns FAILED while comparing two char arguments inside the method [duplicate]

As the tittle, I'm using gmock to test my feature. But one of the issue occurred that EXPECT_CALL always check address of 2 char array instead of their value. Below is my code example:

Base.h

//Create singleton class
class Base {
 private:
  static Base* _ptrInstance;
 public:
  static Base* getInstance();
  void sendString(const char* text, int value);
};

Base.cpp

#include "Base.h"
Base* Base::_ptrInstance = NULL;
Base* Base::getInstance(){
   if ( NULL == _ptrInstance ){
      _ptrInstance = new Base();
   }
   return _ptrInstance ;
}
void Base::sendString(const char* text, int value){
 //do something
}

Here is the function that need to be tested: test.cpp

#include <iostream>
#include "Base.h"
int Test(){
 Base* poBase;
 char text[] = "hello_world";
 poBase->getInstance()->sendString(text, 0);
 return 0;
}

my MOCK method:

MOCK_METHOD2(sendString, void (const char* text, int value));

here is my test case:

TEST_F(myTest, sendStringTest){
EXPECT_CALL(*BaseMock, sendString("hello_world", 0));
Test();
}

When I execute my test. It always return above test case FAILED:

Expected arg #0: is equal to 0x56e88a0d pointing to "hello_world"
           Actual: 0xffcb1601 pointing to "hello_world"
         Expected: to be called once
           Actual: never called - unsatisfied and active

With given failure, I though that EXPECT_CALL is comparing argument addresses instead of their value. (Here, text[] address created in Test.cpp and "hello_world" address inside EXPECT_CALL)

Is anyone know how to overcome this failure? Many thanks.



No comments:

Post a Comment