ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

c – 模拟非虚方法,给出编译错误

2019-10-09 07:06:29  阅读:151  来源: 互联网

标签:c googletest googlemock


我需要编写gtest来测试一些具有非虚方法的现有代码,因此我使用下面的源代码进行测试,但是我得到了编译错误

package/web/webscr/sample_template_class3.cpp: In function âint main()â:
package/web/webscr/sample_template_class3.cpp:64: error: âclass Templatemyclassâ has no member named âgmock_displayâ

sample_template_class3.cpp

#include <iostream>
#include <gtest/gtest.h>
#include <gmock/gmock.h>

using namespace std;

template < class myclass>
class Templatemyclass
{
    private:
            myclass T;
    public :

        void display()
        {
            T.display();
        }

};

class Test
{
    public:
         void display()
        {
            cout<<"Inside the display Test:" <<endl;
        }

};

class MockTest

{
    public:
                MOCK_METHOD0(display,void());
};

class FinalTest
{
        public:
                        void show( Templatemyclass<Test> t)
                        {
                                t.display();
                                cout<<"Inside the display FinalTest:" <<endl;
                        }



};
int main()
{


FinalTest test1;
Templatemyclass<Test> obj1;
Templatemyclass<MockTest> obj2;
EXPECT_CALL(obj2,display()).Times(1);
test1.show(obj1);

return 1;
}

解决方法:

您的代码中存在一些问题.我在下面对其进行了更改,并通过解释对代码进行了评论.如果这还不够清楚,请添加评论,我会尝试进一步解释.

#include <iostream>
#include <gtest/gtest.h>
#include <gmock/gmock.h>

using namespace std;

template <class myclass>
class Templatemyclass {
 private:
  // Hold a non-const ref or pointer to 'myclass' so that the actual
  // object passed in the c'tor is used in 'display()'.  If a copy is
  // used instead, the mock expectations will not be met.
  myclass* T;
 public :
  // Pass 'myclass' in the c'tor by non-const ref or pointer.
  explicit Templatemyclass(myclass* t) : T(t) {}
  void display() { T->display(); }
};

class Test {
 public:
  void display() { cout << "Inside the display Test:" << endl; }
};

class MockTest {
 public:
  MOCK_METHOD0(display, void());
};

class FinalTest {
 public:
  // Templatise this function so we can pass either a Templatemyclass<Test>
  // or a Templatemyclass<MockTest>.  Pass using non-const ref or pointer
  // again so that the actual instance with the mock expectations set on it
  // will be used, and not a copy of that object.
  template<class T>
  void show(T& t) {
    t.display();
    cout<<"Inside the display FinalTest:" <<endl;
  }
};

int main() {
  Test test;
  Templatemyclass<Test> obj1(&test);

  MockTest mock_test;
  Templatemyclass<MockTest> obj2(&mock_test);
  EXPECT_CALL(mock_test,display()).Times(1);

  FinalTest test1;
  test1.show(obj1);
  test1.show(obj2);

  return 0;
}

以下内容可能会简化案例:

#include <iostream>
#include <gtest/gtest.h>
#include <gmock/gmock.h>

template <class myclass>
class Templatemyclass {
 public:
  myclass T;
  void show() const { T.display(); }
};

struct Test {
  void display() const { std::cout << "Inside the display Test:\n"; }
};

struct MockTest {
  MOCK_CONST_METHOD0(display, void());
};

int main() {
  Templatemyclass<Test> obj1;
  obj1.show();

  Templatemyclass<MockTest> obj2;
  EXPECT_CALL(obj2.T, display()).Times(1);
  obj2.show();

  return 0;
}

标签:c,googletest,googlemock
来源: https://codeday.me/bug/20191009/1877452.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有