Search code examples
c++googlemock

Attempting to Reference a Deleted Function UnitTest


I want to create a unit test for a class with a virtual method that takes a template parameter as an argument, and I am having trouble compiling my code. I have included a minimal example of what I am trying to achieve. I cannot make it compile What am i missing?

tst_templates.h file.

    #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 File

#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


Solution

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

    As a class, the methods Print and ~IView are private and thus you cannot access them even from a derived class. Make them public or change class to struct to change the default visibility to public