尝试引用已删除的功能单元测试

时间:2018-06-26 13:12:49

标签: c++ gmock

我想用一个以模板参数作为参数的虚拟方法为一个类创建一个单元测试,而我在编译代码时遇到了麻烦。我提供了一个我要实现的最小示例。我无法编译它我缺少什么?

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

enter image description here

1 个答案:

答案 0 :(得分:1)

template <typename T>
class IView
{
    virtual void Print(const T& settings) = 0;
    virtual ~IView(){}
};

作为class,方法Print~IView是私有的,因此即使从派生类中也无法访问它们。将其设置为public或将class更改为struct,以将默认可见性更改为public