首页 > 解决方案 > 来自外部类的模拟静态方法(我无法更改!)

问题描述

我想模拟(使用 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 方法?

标签: c++unit-testinggooglemock

解决方案


静态依赖注入和 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),但允许在测试时注入模拟或存根类。


推荐阅读