Mock function with arguments as pointer

445 Views Asked by At

I have a function:

void setData(int *ptr) {
   *ptr = 3
};

Can I use Hippomock to mock this function and set the value of ptr? Something like: mock.OnCallFunc(setData).With(int *ptr).Do({ *ptr = 5;});

So I can do something like this later

int p;
setData(&p);
printf("value of p is suppose to be 5: %d\n", p);
1

There are 1 best solutions below

0
hidayat On BEST ANSWER

You can write with a normal function or with a lambda

void setDataMock(int *ptr) {
  *ptr = 5;
}
MockRepository mocks;
mocks.OnCallFunc(setData).Do(setDataMock);
// Or
mocks.OnCallFunc(setData).Do([](int *ptr) {
  *ptr = 5;
});

int p;
setData(&p);
printf("Value of p: %d\n", p);