Search code examples
c++unit-testinghippomocks

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

Solution

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