我正在研究Gmock(Google测试框架)。
我遇到了一个我认为Gmock可以解决但不能解决的问题。请看一下:

#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();
}

我只关注第4个测试,我认为Gmock可以处理第四个TEST:show_text调用display,为什么在这种情况下不能模拟display呢? Gmock im 是否可以模拟在同一个类中的另一个方法中调用的方法,我在第四个测试用例中犯了某个错误?我得到了这样的失败测试结果:
../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

当然,show_textdisplay()是精确调用的。
有人有主意吗?

最佳答案



您不仅成功模拟了display,而且还成功模拟了show_text。您不应依赖于模拟实现的行为,因为它不会被调用。



可能,但是远非如此,您可以在下面找到示例。注意,仅在测试时需要进行一些折衷。使DisplayController::display一个虚拟类可能是最大的类。

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);
}

10-05 19:00
查看更多