Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking side effect for const pointer parameter using googlemock

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.

like image 481
anhoppe Avatar asked Apr 12 '26 09:04

anhoppe


1 Answers

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));
like image 93
rmhartog Avatar answered Apr 14 '26 22:04

rmhartog



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!