[英]Practical example for Dependency Injection and mocking with Googlemock
我正在尋找一個簡單的完整剪切和粘貼示例,用於依賴注入和 mocking 使用 Googlemock。 我發現了幾個關於這個問題的理論討論,這些討論用代碼片段解釋了它是如何工作的,但無法找到一個完整的運行示例來剪切和粘貼並嘗試。 有什么可用的嗎?
為了了解它如何與 Googlemock 一起工作,我制作了這個完整的示例,我想與其他初學者分享該主題。 根據其他問答理論背景假設。 我在 Debian Bullseye 系統上運行它。
在一個虛構的庫中有一個 class Mylib
,該方法只返回123
。 在測試中,它被模擬返回456
。 真正的 class 和 mocking class 都繼承自一個接口 ZA2F2ED4F8EBC2CBB4C21A29DC40AB6DZ。 這確保了由注入器( TEST(..){..}
宏)注入到應用程序Myapp
的不同對象具有相同的方法調用。 這是示例:
~$ cat test_myapp.cpp
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include <iostream>
class MylibInterface {
public:
virtual ~MylibInterface() {}
virtual int func() = 0;
};
class Mylib : public MylibInterface {
public:
virtual ~Mylib() {}
int func() override {
return 123;
}
};
class MylibMock : public MylibInterface {
public:
virtual ~MylibMock() {}
MOCK_METHOD(int, func, (), (override));
};
class Myapp {
// this pointer will be injected by the injector either with pointing
// to the real object or to the mock object. The interface ensures that both
// objects have the same method calls.
MylibInterface* m_mylib;
public:
Myapp(MylibInterface* mylib) : m_mylib(mylib) {}
bool func() {
int ret = m_mylib->func();
std::cout << "mylib.func returns: '" << ret << "'\n";
return true;
}
};
TEST(MylibTestSuite, mock_mylib_func)
// this test macro can be seen as the injector. It determins what object
// is injected to myapp.
{
using ::testing::Return;
// inject a real mylib object to myapp and exersize it
Mylib mylib;
Myapp myapp(&mylib);
std::cout << " real ";
EXPECT_TRUE(myapp.func());
// inject a mocked mylib object to myapp
MylibMock mylib_mock;
Myapp myapp_mock(&mylib_mock);
EXPECT_CALL(mylib_mock, func())
.WillOnce(Return(456));
// and exersize it
std::cout << "mocked ";
EXPECT_TRUE(myapp_mock.func());
}
int main(int argc, char** argv) {
::testing::InitGoogleMock(&argc, argv);
return RUN_ALL_TESTS();
}
我編譯它:
~$ /usr/bin/g++ -std=c++11 -pedantic-errors -Wall -o test_myapp.a -I$BUILD_DIR/googletest-src/googletest/include -I$BUILD_DIR/googletest-src/googlemock/include test_myapp.cpp $BUILD_DIR/lib/libgtestd.a $BUILD_DIR/lib/libgmockd.a -lpthread
執行測試時,它應該如下所示:
~$ ./test_myapp.a
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from MylibTestSuite
[ RUN ] MylibTestSuite.mock_mylib_func
real mylib.func returns: '123'
mocked mylib.func returns: '456'
[ OK ] MylibTestSuite.mock_mylib_func (0 ms)
[----------] 1 test from MylibTestSuite (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[ PASSED ] 1 test.
Ingo 的示例使用 std::shared_ptr。
// Ingo's answer to Dependency Injection and mocking modified to
// use std::shared_ptr.
#include <iostream>
#include <memory>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
class MylibInterface {
public:
virtual ~MylibInterface() {}
virtual int func() = 0;
};
class Mylib : public MylibInterface {
public:
virtual ~Mylib() {}
int func() override {
return 123;
}
};
using MyLibPtr = std::shared_ptr<MylibInterface>;
class MylibMock : public MylibInterface {
public:
virtual ~MylibMock() {}
MOCK_METHOD(int, func, (), (override));
};
class Myapp {
// this pointer will be injected by the injector either with pointing
// to the real object or to the mock object. The interface ensures that both
// objects have the same method calls.
// MylibInterface* m_mylib;
MyLibPtr m_mylib;
public:
Myapp(MyLibPtr mylib) : m_mylib(mylib) {}
bool func() {
int ret = m_mylib->func();
std::cout << "mylib.func returns: '" << ret << "'\n";
return true;
}
};
TEST(MylibTestSuite, mock_mylib_func)
// this test macro can be seen as the injector. It determins what object
// is injected to myapp.
{
using ::testing::Return;
// inject a real mylib object to myapp and exersize it
Myapp myapp(std::make_shared<Mylib>());
std::cout << " real ";
EXPECT_TRUE(myapp.func());
// inject a mocked mylib object to myapp
//MylibMock mylib_mock;
auto lp = std::make_shared<MylibMock>();
Myapp myapp_mock( lp );
//EXPECT_CALL(mylib_mock, func())
EXPECT_CALL( *lp, func())
.WillOnce(Return(456));
// and exersize it
std::cout << "mocked ";
EXPECT_TRUE(myapp_mock.func());
}
/*
int main(int argc, char** argv) {
::testing::InitGoogleMock(&argc, argv);
return RUN_ALL_TESTS();
}
*/`
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.