c++ - 来自外部类的模拟静态方法(我无法更改!)
问题描述
我想模拟(使用 gmock)来自我无法更改的类的静态函数。A 是我要模拟的类:
Class A
{
public:
static std::string get_id();
...
}
B 是我想用 gmock 测试的课程:
Class B
{
public:
B(A *a_ptr);
...
std::string foo();
private:
A *m_a_ptr;
}
B::B(A *a_ptr) : m_a_ptr(a_ptr)
{
}
std::string B::foo()
{
id = m_a_ptr->get_id();
return id;
}
如何在不更改 A 类的情况下模拟 get_id 方法?
解决方案
静态依赖注入和 GMock 委托
我们将首先将您的示例最小化为以下内容(以使后面的段落尽可能不嘈杂):
// a.h
#include <string>
// class to mock
class A {
static std::string get_id();
};
// b.h
#include <string>
#include "a.h"
// class that use A
struct B {
std::string foo() const {
return A::get_id();
}
};
虽然您无法更改A
,但您可以更改B
为静态注入 A
产品代码,而您可以静态注入A
测试代码的模拟委托:
// b.h
#include <string>
#include "a.h"
namespace detail {
// The type template parameter is set to A by default,
// and should not need to override this default type
// in production code, but can be injected with
// mocked classes in test code.
template<typename AImpl = ::A>
struct BImpl {
std::string foo() const {
return A::get_id();
}
};
} // namespace detail
// Expose product-intent specialization.
using B = BImpl<>;
A
使用静态(非线程安全)方法模拟对注入的静态类型的调用的模拟:
// a_mock.h
#include <memory>
#include <string>
#include "gmock/gmock.h"
class AMock {
// Mocked methods.
struct Mock {
MOCK_CONST_METHOD0(get_id,
std::string());
};
// Stubbed public API for static function of object under test:
// delegates stubbed calls to the mock.
static std::string get_id() {
if (const auto mock = mock_.lock()) {
mock->get_id();
}
else {
ADD_FAILURE()
<< "Invalid mock object! The test can no "
"longer be considered useful!";
}
}
// Public setter to specify the mock instance used in test (which in
// turn will be the instance that Google Test's EXPECTS and mocked
// calls is placed upon).
static void setMock(const std::shared_ptr<Mock>& mock) { mock_ = mock; }
private:
// Pointer to mock instance.
static std::weak_ptr<Mock> mock_;
};
最后,它可以用于以下测试BImpl
:
// b_test.cpp
#include "b.h" // object under test
#include "gmock/gmock.h"
#include "a_mock.h"
class BImplTest : public ::testing::Test {
public:
using BImplUnderTest = BImpl<AMock>;
BImplTest() : amock_(std::make_shared<AMock::Mock>()) {
AMock::setMock(amock_);
}
};
TEST_F(BImplTest, foo) {
// Setup mocked call(s).
EXPECT_CALL(amock_, foo()).WillOnce(::testing::Return( /*...*/ ));
// Call object under test.
BImplUnderTest b{};
b.foo();
}
进一步隐藏了实际上B
是类模板的特化的事实BImpl
如果你开始大量使用这种模式(在不同的子例程上以滑动窗口的方式)并且想要避免单个大而臃肿的翻译单元,你可以将detail::B
类模板的成员函数的定义移动到单独的标题中,比如b-timpl.h
(包括b.h
)并在与 关联的源文件中b.h
,例如b.cpp
,包含b-timpl.h
而不是b.h
为生产意图detail::BImpl
专业化添加显式实例化定义:
// b.cpp
template class ::detail::BImpl<>;
而在您的测试中::detail::BImpl
包括b-timpl.h
而不是为b.h
类模板的模拟注入专业化添加显式实例化定义:
// b_test.cpp
#include "b-timpl.h"
// ...
template class ::detail::BImpl<AMock>;
// ...
为什么?该类BImpl
未参数化以允许其接口的用户静态注入不同的行为(对于用户意图,用户应该只看到B
),但允许在测试时注入模拟或存根类。
推荐阅读
- android - 带有操作和下拉菜单的 Android 通知
- amazon-web-services - 来自旧版本 ssm sdk (1.11.68) 的 AWS Parameter Store
- scala - 从 seq 列和值动态生成过滤器
- ios - 快速从远程资源加载 svg
- javascript - 安装 React Native 地图。在 UIManager 中找不到 AIRMap
- node.js - 如果客户端关闭会话,nodejs v8.11.3 使用 TLS 在 http2 中中止
- node.js - Electron desktopCapturer 到 Web API 频率全为 0
- jquery - jQuery 选择器 - 选择以相同数字结尾的类的元素
- google-apps-script - 只有一个 TRUE 复选框
- spring - Spring Autowired 共享队列 NullPointerException