Search code examples
c++testingc-stringsgooglemock

C-string will never be called in gmock


I have a C++ function to test, and inside this C++ function it will call a C function. I would like to mock this C function using Gmock. C function's signature is:

int functionA(const char key[3],char value[6]);

The int is a return code, and input is key, output is value. I want to mock the return value and set the second argument.

C++ function I would like to test is:

int functionB{
     ....
     int rcode = functionA(key, value);
     .......
}

My "mock.h" is:

#include<functionA.h>
#include <gmock/gmock.h>
class function_A_interface {
public:
   virtual int functionA(const char key[3],char value[6]) = 0;
};

class function_A_mock : public function_A_interface {
public:
   MOCK_METHOD2(functionA, int(const char[3], char[6]));
}

My "mock.cc" is:

#include <function_A_mock.h>
extern function_A_mock MockObj;
int function_A(const char key[3],char value[6])
{
    return MockObj.functionA(key, value);
}

My "test.t.cpp" is:

TEST(Test, tes){

function_A_mock MockObj;
 pa[3] = {0};
 pb[6] = {1};
 EXPECT_CALL(MockObj, functionA(pa, pb)).WillOnce(DoAll(SetArgPointee<1>(*set), Return(3)));

  functionB(parameter1, parameter2); // it will convert these two paramters to according input in functionA
}

but it shows me:

Unexpected mock function call - returning default value.
    Function call: functionA(ffbfc5b0 pointing to "abc      E", ffbfc570 pointing to "")
          Returns: 0
Google Mock tried the following 1 expectation, but it didn't match:

test.t.cpp:121: EXPECT_CALL(MockObj, functionA(pa, pb))...
  Expected arg #0: is equal to ffbfd380 pointing to "abc      E"
           Actual: ffbfc5b0 pointing to "abc      E"
  Expected arg #1: is equal to ffbfd340 pointing to ""
           Actual: ffbfc570 pointing to ""
         Expected: to be called once
           Actual: never called - unsatisfied and active

It seems I could never match the parameters I set so it will never be called. How to mock the C string for both match and set argument purpose in my case? Thanks.


Solution

  • If you don't provide any other Matcher to mock function arguments, Eq() is assumed. Eq() matcher is very simple - it compares argument it received with the expected argument using operator ==. But for C-style strings this comparison compares pointers, not actual strings.

    You should use StrEq matcher to compare instead:

    using ::testing::StrEq;
    EXPECT_CALL(MockObj, functionA(StrEq(pa), StrEq(pb)))
           .WillOnce(DoAll(SetArgPointee<1>(*set), Return(3)));
    

    However, StrEq() will compare things as null-terminated strings. If you want to compare whole arrays instead, use ElementsAreArray(array, count):

    EXPECT_CALL(MockObj, functionA(ElementsAreArray(pa, 3), ElementsAreArray(pb, 6)))
           .WillOnce(DoAll(SetArgPointee<1>(*set), Return(3)));