rollbear / trompeloeil

Header only C++14 mocking framework
Boost Software License 1.0
802 stars 85 forks source link

How to mock a function that operates on the value passed in by reference/pointer #307

Closed lowkin18 closed 1 year ago

lowkin18 commented 1 year ago

I am unclear on how I can mock a function that takes a pointer and passes back through this pointer.

for example

void get_current(float *current)
{
     *current = 10
}

how do I create the mock expectation for this? ALLOW_CALL(mockObj, get_current(_))??

I need to be able to set the returned current to some values for passing/failing conditions

Any help would be appreciated.

rollbear commented 1 year ago

Assign to it using the positional parameters, for example in a .SIDE_EFFECT(). _1 will be the pointer. *_1 is whatever it points to.

Example: https://godbolt.org/z/WGcqva164

rollbear commented 1 year ago

I wouldn't call this frequent, but you're not the first to ask, so I've added an entry to the FAQ.

lowkin18 commented 1 year ago

Thank you very much!