mardi 11 juin 2019

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. So 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", so 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);
}

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

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

Aucun commentaire:

Enregistrer un commentaire