具有多个模板参数的模拟函数

问题描述 投票:0回答:1

我想嘲笑

std::make_shared

#include <memory>
#include <string>
#include <gmock/gmock.h>
#include <gtest/gtest.h>

template <typename T, typename... Args>
class MakeSharedMock {
 public:
  MOCK_METHOD(std::shared_ptr<T>, MakeShared, (Args... args), (const noexcept));
};

int main(){
    MakeSharedMock<std::string> mock_make_shared;
}

但是出现这样的错误

error: static assertion failed: This method does not take 1 arguments.
Parenthesize all types with unproctected commas.   
MOCK_METHOD(std::shared_ptr<T>, MakeShared, (Args... args), (const
noexcept));

如果我先省略

typename T
,它会编译

template <typename... Args>
class MakeSharedMock {
 public:
  MOCK_METHOD(std::shared_ptr<int>, MakeShared, (Args... args), (const noexcept));
};

我想知道是否可以使用多个模板参数来模拟函数。

c++ googletest googlemock
1个回答
0
投票

GMock 似乎不支持可变参数模板,但您可以使用专业化作为解决方法:

template <typename T, typename... Args>
class MakeSharedMock;

template <typename T>
class MakeSharedMock<T>
{
 public:
  MOCK_METHOD(std::shared_ptr<T>, MakeShared, (), (const noexcept));
};

template <typename T, typename T1>
class MakeSharedMock<T, T1>
{
 public:
  MOCK_METHOD(std::shared_ptr<T>, MakeShared, (T1), (const noexcept));
};

template <typename T, typename T1, typename T2>
class MakeSharedMock<T, T1, T2>
{
 public:
  MOCK_METHOD(std::shared_ptr<T>, MakeShared, (T1, T2), (const noexcept));
};

// ...

演示

© www.soinside.com 2019 - 2024. All rights reserved.