gpt4 book ai didi

c++ - Google 测试 - 构造函数声明错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:48:01 24 4
gpt4 key购买 nike

我正在尝试从具有构造函数声明(带参数)的普通类创建测试夹具类,如下所示:

你好.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 文件中。这样做有什么办法吗?我尝试过以多种方式实现它,但到目前为止还没有成功。关于如何实现这个有什么建议吗?

最佳答案

此错误告诉您您没有在 helloTestFixture 类中提供默认构造函数,TEST_F 宏需要它来创建您的类的对象。

您应该使用部分 关系而不是是一个。创建您需要的 hello 类的所有对象,以便测试您需要的所有各个方面。

我不是 Google 测试方面的专家。但是,在这里浏览文档:

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

似乎首选SetUp 方法。如果您的目标是测试类 hello,您可以这样写:

#include <memory>

#include "hello.h"
#include "gtest.h"

class TestHello: public testing::Test {
public:
virtual void SetUp()
{
obj1.reset( new hello( /* your args here */ ) );
obj2.reset( new hello( /* your args here */ ) );
}

std::auto_ptr<hello> obj1;
std::auto_ptr<hello> obj2;
};

TEST_F(QueueTest, MyTestsOverHello) {
EXPECT_EQ( 0, obj1->... );
ASSERT_TRUE( obj2->... != NULL);
}

auto_ptr 并不是真正需要的,但它会节省您编写 TearDown 函数的工作量,并且它还会在出现问题时删除对象。

希望这对您有所帮助。

关于c++ - Google 测试 - 构造函数声明错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8344237/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com