Search code examples
c++googlemock

Gmock can't mock a method that called in another method in the same class


I am investigating Gmock (Google Test Framework). I run into an issue which I think Gmock can resolve it but It cannot. Please have a look to:

#include <iostream>
#include <gtest/gtest.h>
#include <gmock/gmock.h>

using namespace std;
using ::testing::Invoke;  

//---- THINGS TO TEST ---------
class MyTest {
public:
    void display(const char* str);
    void show_text();
};

void MyTest::display(const char* str){
    cout << __func__<< " " << str << "\n";
    cout << ":-->Inside the display myTest\n";
}

void MyTest::show_text(){
    display("INSIDE MYTEST"); // HOW to mock display() here ?
}
//-------------------------------

//--- Configuration to test myTest ----
template <class myclass>
class Caller {
public:
    myclass T;
    void call_show_text() ;
    void call_display(const char* s) ;
};

template <class myclass>
void Caller<myclass>::call_show_text()
{
    T.show_text();
}

template <class myclass>
void Caller<myclass>::call_display(const char* str)
{
    T.display(str);
}

void my_display(const char* str){
    cout << __func__<< " OUTSIDE MYTEST\n" <<":-->Outside the display myTest\n";
}

struct MockTest {
    MOCK_METHOD1(display, void(const char*));
    MOCK_METHOD0(show_text, void());
};
//-------------------------------

//----- Test cases -------------
//----- Test cases -------------
TEST(TestShowTextMethod, CallDisplayOfmyTest){
    Caller<MyTest> obj1;
    obj1.call_show_text();
}

TEST(TestShowTextMethod, ReroutingDisplayToNewFunctionWithCallDisplay){
    Caller<MockTest> obj2;
    EXPECT_CALL(obj2.T, display("test_obj_2"))
              .Times(1)
              .WillOnce(Invoke(my_display));
    obj2.call_display("test_obj_2");
}

TEST(TestShowTextMethod, ReroutingDisplayToNewFunctionWithCallShowText){
    Caller<MockTest> obj3;
    EXPECT_CALL(obj3.T, display("test_obj_3"))
              .Times(1)
              .WillOnce(Invoke(my_display));
    obj3.call_display("test_obj_3");
}

TEST(TestShowTextMethod, ReroutingToNewFunctionWithCallShowText){
    const char * str = "INSIDE MYTEST";
    Caller<MockTest> obj4;

    EXPECT_CALL(obj4.T, show_text());

    EXPECT_CALL(obj4.T, display(str))
              .Times(1)
              .WillOnce(Invoke(my_display));
    obj4.call_show_text();
}
//-------------------------------

//--- Initialization for test ----
int main(int argc, char** argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

Only focus on the 4th test, I think Gmock can handle the fourth TEST: show_text calls display, why I can't mock display in this case ? Is Gmock impossible to mock a method is called in another method in the same class or I made a certain mistake in the 4th testcase ? I got a failed test-result like this:

../test_program.cpp:100: Failure
Actual function call count doesn't match EXPECT_CALL(obj4.T, display(str))...
         Expected: to be called once
           Actual: never called - unsatisfied and active

Of course, show_text and display() are exactly called. Does anybody have an idea ?


Solution

  • why I can't mock display in this case?

    You have not only successfully mocked display but also show_text as well. You should not lean on the behaviour of mocked implementation, because it will not be called.

    Is Gmock impossible to mock a method is called in another method in the same class.

    It is possible, but far from pretty, You can find example below. Note that there are some trade-offs made only for tests. Making DisplayController::display a virtual class is probably the biggest one.

    class DisplayController
    {
    public:
        virtual void display(const std::string& text)
        {
    
            cout << text << endl;
        }
        void show_str(const std::string& text)
        {
            cout << "Start" << endl;
            display(text);
            cout << "End" << endl;
        }
    };
    
    class TestableDisplayController : public DisplayController
    {
    public:
        MOCK_METHOD1(display, void(const std::string&));
        MOCK_METHOD1(show_str_called, void(const std::string&));
    
        void show_str(const std::string& text)
        {
            show_str_called(text);
            DisplayController::show_str(text);
        }
    };
    
    template <typename T>
    class ClassUnderTest
    {
    public:
        ClassUnderTest(T& display)
            : displayController(display)
        {}
    
        void callDirectDisplay(const std::string& text)
        {
            displayController.display(text);
        }
    
        void callIndirectDisplay(const std::string& text)
        {
            displayController.show_str(text);
        }
    
    private:
        T& displayController;
    };
    
    TEST(test, example)
    {
        TestableDisplayController mockedDisplayController;
        ClassUnderTest<TestableDisplayController> sut(mockedDisplayController);
    
        std::string l_directDisplay = "directDisplay";
        std::string l_indirectDisplay = "indirectDisplay";
    
        EXPECT_CALL(mockedDisplayController, display(l_directDisplay));
        sut.callDirectDisplay(l_directDisplay);
    
        EXPECT_CALL(mockedDisplayController, display(l_indirectDisplay));
        EXPECT_CALL(mockedDisplayController, show_str_called(l_indirectDisplay));
    
        sut.callIndirectDisplay(l_indirectDisplay);
    }