我想用一个以模板参数作为参数的虚拟方法为一个类创建一个单元测试,而我在编译代码时遇到了麻烦。我提供了一个我要实现的最小示例。我无法编译它我缺少什么?
tst_templates.h文件。
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <gmock/gmock-matchers.h>
#include "mockview.h"
#include "configuration.h"
using namespace testing;
class MyTest : public ::testing::Test
{
public:
protected:
FRIEND_TEST(MyTest, Print);
virtual void SetUp()
{
}
virtual void TearDown()
{
}
};
TEST_F(MyTest, Print)
{
MockView<Configuration> View;
Configuration c;
View.Print(c);
}
MockView.h文件
#ifndef MOCKVIEW_H
#define MOCKVIEW_H
#include <gmock/gmock.h>
#include "iview.h"
template <typename T>
class MockView : IView<T>
{
public:
MOCK_METHOD1_T(Print, void(const T &Settings));
virtual ~MockView(){}
};
#endif // MOCKVIEW_H
Configuration.h
#ifndef CONFIGURATION_H
#define CONFIGURATION_H
#include "iconfiguration.h"
#include <iostream>
class Configuration : public IConfiguration
{
public:
Configuration()
{
}
void Print()
{
std::cout << "Configuration Print" << std::endl;
}
~Configuration()
{
}
};
#endif // CONFIGURATION_H
IView.h
#ifndef IVIEW_H
#define IVIEW_H
template <typename T>
class IView
{
virtual void Print(const T& settings) = 0;
virtual ~IView(){}
};
#endif // IVIEW_H
答案 0 :(得分:1)
template <typename T>
class IView
{
virtual void Print(const T& settings) = 0;
virtual ~IView(){}
};
作为class
,方法Print
和~IView
是私有的,因此即使从派生类中也无法访问它们。将其设置为public
或将class
更改为struct
,以将默认可见性更改为public