2

I am using Google Mock to unit test my C implementation. For one of my mock function the out parameter is defined as void pointer. The mock function is given below:

MOCK_METHOD3(file_read, int(const char *file_name, const char *type_name, void     *data_p));

According to How to set a value to void * argument of a mock method in google mock testing?

I created an ACTION_P

ACTION_P(SetArg2ToMCValue, value) {   reinterpret_cast<void *>(arg2) = value; }

In my test code I set the default value to the parameter that is cast to void in ACTION_P and my expectation

  struct.a = 5.0;
  struct.b = 15.0;
  //Expectations
EXPECT_CALL(*libfile_mock,  file_read(_,_,_)).WillOnce(DoAll(SetArg2ToMCValue(&struct), Return(0)));

When the test is run, I do not see the custom values, I set to the struct. Rather I see 0. How do I set value to an out parameter that is also a void pointer in Google Mock?

Community
  • 1
  • 1
  • After some search I found that arg2 will already be a void point and hence I need to cast it the struct type within ACTION_P ACTION_P(SetArg2ToMCValue, value) { *reinterpret_cast(arg2) = *value; } – Ganesh Jayachandran Jun 02 '15 at 18:48

1 Answers1

2

After some search I found that arg2 will already be a void pointer and hence I need to cast it to the struct type within ACTION_P.

ACTION_P(SetArg2ToMCValue, value) { *reinterpret_cast<struct *>(arg2) = *value; } 

This worked.