Samantha
Samantha

Reputation: 294

Mock function with arguments as pointer

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);

Upvotes: 4

Views: 498

Answers (1)

hidayat
hidayat

Reputation: 9753

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);

Upvotes: 4

Related Questions