Google 测试 - 构造函数声明错误
我正在尝试从带有构造函数声明(带参数)的普通类创建一个测试夹具类,如下所示:
hello.h
class hello
{
public:
hello(const uint32_t argID, const uint8_t argCommand);
virtual ~hello();
void initialize();
};
其中 uint32_t 是: typedef unsigned int
和 uint8_t是: typedef unsigned char
我的测试夹具类:
helloTestFixture.h
class helloTestFixture:public testing::Test
{
public:
helloTestFixture(/*How to carry out the constructor declaration in this test fixture class corresponding to the above class?*/);
virtual ~helloTestFixture();
hello m_object;
};
TEST_F(helloTestFixture, InitializeCheck) // Test to access the 'intialize' function
{
m_object.initialize();
}
尝试实现上述代码后,它给了我错误:
Error C2512: no appropriate default constructor available
我试图复制构造函数这hello.h 文件复制到我的 hellotestfixture.h 文件中。有办法做到这一点吗? 我尝试过以多种方式实现它,但到目前为止还没有成功。关于如何实施这一点有什么建议吗?
I am trying to create a test fixture class from a normal class with constructor declaration (with arguments) as shown below:
hello.h
class hello
{
public:
hello(const uint32_t argID, const uint8_t argCommand);
virtual ~hello();
void initialize();
};
where uint32_t is: typedef unsigned int
and uint8_t is: typedef unsigned char
My Test Fixture Class:
helloTestFixture.h
class helloTestFixture:public testing::Test
{
public:
helloTestFixture(/*How to carry out the constructor declaration in this test fixture class corresponding to the above class?*/);
virtual ~helloTestFixture();
hello m_object;
};
TEST_F(helloTestFixture, InitializeCheck) // Test to access the 'intialize' function
{
m_object.initialize();
}
After trying to implement the above code, it gives me the error:
Error C2512: no appropriate default constructor available
I was trying to replicate the constructor constructed in the hello.h file into my hellotestfixture.h file. Any way around for doing that?
I have tried implementing it in many ways but no success as of yet. Any suggestions on how to implement this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此错误告诉您,您没有在
helloTestFixture
类中提供默认构造函数,而TEST_F
宏需要该默认构造函数来创建类的对象。您应该使用part-of关系而不是is-a。创建您需要的类
hello
的所有对象,以便测试您需要的所有各个方面。我不是谷歌测试专家。但是,请浏览此处的文档:
https://github.com/google/googletest/blob/master/googletest/docs/primer.md#test-fixtures-using-the-same-data-configuration-for-multiple-tests
<一href="https://github.com/google/googletest/blob/master/googletest/docs/faq.md#should-i-use-the-constructordestructor-of-the-test-fixture-or-setupteardown" rel =“不关注” noreferrer">https://github.com/google/googletest/blob/master/googletest/docs/faq.md#should-i-use-the-constructordestructor-of-the-test-fixture-or-setupteardown
看来
SetUp
方法是首选。如果您的目标是测试hello
类,您可以这样编写:auto_ptr
并不是真正需要的,但它会节省您编写TearDown
函数,如果出现问题,它也会删除该对象。希望这有帮助。
This error is telling you that you are not providing a default constructor in the
helloTestFixture
class, needed by theTEST_F
macro in order to create an object of your class.You should use a part-of relationship instead of an is-a. Create all objects of the class
hello
you need, in order to test all the various aspects you need.I am not an expert in Google Test. However, browsing the documentation here:
https://github.com/google/googletest/blob/master/googletest/docs/primer.md#test-fixtures-using-the-same-data-configuration-for-multiple-tests
https://github.com/google/googletest/blob/master/googletest/docs/faq.md#should-i-use-the-constructordestructor-of-the-test-fixture-or-setupteardown
It seems that the
SetUp
method is preferred. If your objective is to test the classhello
, you could write it this way:auto_ptr
is not really needed, but it will save you the effort of writing theTearDown
function, and it also will delete the object in case something goes wrong.Hope this helps.
经过不多的代码修正后,以下是我为您准备的内容:答案:)
它可以很好地编译和运行,希望这能回答您的问题。 :)
After not much of code correction, here's what I've got for you in store: An Answer :)
This compiles and runs nicely and hope this answers your question. :)