How to call a method for an object passed into a laughable method

I am new to using the way of working.

I have a mock with the addEvent method, which takes an object of type MyClass with a pointer. I need to call MyClass :: makeCall on this object.

class SchedulerMock
{
public:
    MOCK_CONST_METHOD1(addEvent, void(MyClass*));

};

I found this topic: What is the easiest way to call a member function in an argument passed to the mocked function?

And here is an example:

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

, . , mock makeCall , . , , addEvent schedulerMock, , makeCall , - addEvent . , .

?

+4
2

, mock...

class SchedulerMock
{
  public:
    //.... etc
    void addEvent(MyClass* c) const
    {
      c->makeCall();
      mockAddEvent(c); //Wholla, test still works and makeCall called. 
    }

    MOCK_CONST_METHOD1(mockAddEvent, void(MyClass*));
};
+1

( - , ..). , MyClass *, makeCall :

ACTION(MakeCall)
{
    // arg0 is predefined as 0-th argument passed to the mock function call,
    //  MyClass* in this case
    arg0->makeCall();
}

TEST(...)
{
    // Create mock object and set expectation, and specify to invoke the 
    //  MakeCall Action
    SchedulerMock mock;
    EXPECT_CALL(mock, addEvent(_))
        .WillOnce(MakeCall());

    ...
}

mock.addEvent(my_class_ptr), makeCall MyClass *.

, , , . , MyClass::makeCall int, , , :

class MyClass
{
    void makeCall(int);
};

ACTION_P(MakeCall, value)
{
    arg0->makeCall(value);
}

TEST(...)
{
    const int FIRST_VAL = 10;
    const int SECOND_VAL = 20;

    MyClass my_class_obj;
    SchedulerMock mock;
    EXPECT_CALL(mock, addEvent(_))
        .WillOnce(MakeCall(FIRST_VAL))
        .WillOnce(MakeCall(SECOND_VAL));
    ...
}

. Google:

Google Mock Cheat Sheet -

Google Mock Cookbook -

+1

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


All Articles