0

I would like to execute multiple instructions inside of a .Do().

Something like:

mocks.ExpectCallFunc(mockedFunc).Do(a++;someFunc();)

How can I make this happen?

Dan
  • 61
  • 12
  • 2
    I don't know this framework, but if you can call a function there, then lambda should work as well. (still a function-like element, but can be defined inline and doesn't have any name). – Yksisarvinen Aug 08 '19 at 11:38
  • 1
    Use a comma, instead of a semicolon? I can only guess, since I do not see the function definition – A M Aug 08 '19 at 11:42
  • 2
    Don't know about hippomocks, but gmock has `DoAll()` – sklott Aug 08 '19 at 11:47
  • @ArminMontigny Doubt that works since it appears to take a function pointer (or possibly object, who knows). You could add a comma operator between the two and it would compile but that would just discard the first function. – Max Langhof Aug 08 '19 at 11:48
  • 1
    Are you compiling this as C or C++? The potential solutions to this problem are _very_ different in both cases (well, nonexistent for C)... – Max Langhof Aug 08 '19 at 11:50

1 Answers1

1

You can either create a function to wrap what you need (bit of a overhead if its a function + a++), or you can create and pass a Lambda.

e.g. -

auto additionalFunc = [](<mockedFunc's arguments>) { a++; doFunc() ;};    
mocks.ExpectCallFunc(mockedFunc).Do(additionalFunc)

Note that your lambda must return and accept same type arguments as the mockedFunc (even if it doesn't use them). So if the function you are mocking is bool mockedFunc(std::string&)

auto additionalFunc = [](std::string&) { a++; doFunc() ; return true;};

If you can't use lambda expressions (C++ < 11) then you should probably create a function packing the 2 options you need. Same guidelines apply (keep return value and arguments)

Shaharni
  • 36
  • 3