Question

I'm using googlemock for unit tests and I try to mock a method that has an 'out array parameter':

void MyMock::myFunc(double myVal[2]).

The method myFunc is supposed to store values in the myVal array.

How do I mock this side effect? I tried the following:

double a_mockedValues[] = {1., 2.};
ON_CALL(myMock, myFunc(_)).WillByDefault(SetArgPointee<0>(a_mockedValues));

My intention is that the caller of myFunc receives the values 1. and 2. into the array that it passes to the mocked method.

However, this approach does not work. The compiler says something like:

cannot specify explicit initializer for arrays

Does anybody know how to mock the behavior of such a parameter?

Thank you.

Was it helpful?

Solution

There is actually a predicate for this specific use-case: SetArrayArgument (see Google Mock CookBook

Your code would then become:

double a_mockedValues[] = { 1., 2. };
ON_CALL(myMock, myFunc(_)).WillByDefault(SetArrayArgument<0>(a_mockedValues, a_mockedValues + 2));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top