我需要写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;
}
弗雷泽,你是伟大的!!!!!! :)非常感谢它正在工作.. – Gopal
弗雷泽的另一个真棒答案!嘿,弗雷泽,我正在四处寻找有关如何在不使用模板的情况下模拟非虚拟方法的信息。我发现了一些非常有趣的答案,包括这两个:http://stackoverflow.com/a/2857770/1735836和http://stackoverflow.com/q/1127918/1735836。很想听听你的想法! – Patricia
我想我会倾向尝试HippoMocks,但我在这里没有亲身经历。我们在代码库中使用了GoogleMock一段时间,并且设置和维护测试非常痛苦。例如,它对C++ 11智能指针确实不起作用。我听说过的另一种可能性是http://www.typemock.com/isolatorpp-product-page。这应该避免你为了测试而改变你的产品代码,但我不认为它是免费的,而且我没有亲身经历。除了那些,我会选择两个选项之一... – Fraser