What is the easiest way to call a member function in an argument passed to the mocked function?

Given the interfaces

class IFooable {
  virtual void Fooable() = 0;
};

class IFoo {
  virtual void Foo(IFooable* pFooable) = 0;
};

and cartoon false layout

class TMockFoo : public IFoo {
  MOCK_METHOD1(Foo, void (IFooable*));
};

What is the easiest way to specify an action that invokes Fooable()an argument for a prepared method Foo()?

I tried

TMockFoo MockFoo;
ON_CALL(MockFoo, Foo(_))
  .WithArg<0>(Invoke(&IFooable::Fooable));

but this does not compile because Invoke()with one argument it expects a free function, not a member function.

Usage boost::bindshould probably work, but not necessarily make the code too readable. Before writing a custom one Action, I wanted to check if I had disappeared something completely obvious.

+3
source share
3 answers

, ,

TMockFoo MockFoo;
ON_CALL(MockFoo, Foo(_))
  .WillByDefault(Invoke(boost::mem_fn(&IFooable::Fooable)));
+1

, Google Mock, , Invoke , , :

IFooable* ifooable = new IFooableImpl(...);
TMockFoo MockFoo;
ON_CALL(MockFoo, Foo(_))
  .WithArg<0>(Invoke(&ifooable,&IFooable::Fooable));
0

The easiest way to call a member function is to use lambda:

   TMockFoo MockFoo;
   ON_CALL(MockFoo, Foo(_))
      .WithArg<0>(Invoke([](IFooable * pFooable) { pFooable->Fooable(); }));
0
source

Source: https://habr.com/ru/post/1615875/


All Articles