1

I have this code in C++11

struct Callbacks
{
    using StartFunc = std::function<bool()>;
    StartFunc start;

    using FinishFunc = std::function<bool(int status)>;
    FinishFunc finish;
};

// this registers the callbacks and calls the 2 functions 
int do_something(const Callbacks& callbacks)
{
   ...
   callbacks.start(true);
   
   ...
   callbacks.finish(true);  
}

And I want to mock the callbacks, to make sure they are called properly. I read How to use gmock to mock up a std::function? and I did something like this

MockFunction<Callbacks::start> startFuncMock;
MockFunction<Callbacks::stop> finishFuncMock;    

but when I call my function

Callbacks cb;
cb.start = startFuncMock; // this does not compile

do_something(cb);

How can I make it work? thanks

273K
  • 29,503
  • 10
  • 41
  • 64
Taw
  • 479
  • 3
  • 15
  • What error do you get? – Ben Sep 29 '22 at 17:23
  • You didn't seem to read carefully the answer in the linked question. It should be `cb.start = startFuncMock.AsStdFunction();`. The question has been closed as a dup. – 273K Sep 30 '22 at 00:11
  • 1
    Please don't use the tag [gmock] anymore, it's a Groovy mocking framework. You again didn't seem to read carefully the tag description. Always reading information casually is not a good habit. – 273K Sep 30 '22 at 00:17

0 Answers0