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

标签 c++ visual-c++ declaration googletest default-constructor

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

你好.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/

相关文章:

c++ - 如何将带有数据的 header 添加到 Qt 中的 QTableWidget?

c++ - 为什么不总是构建带有调试信息的版本?

c++ - 你应该: #import or "add new reference"?

C++ 在 else/if 中声明一个优先级队列?

C#类函数成员声明与实现

c++ - 如何指示结构成员没有别名?

c++ - 获取操作系统是 32 位还是 64 位

c++ - 如何在 C++ 中检查无限和不确定的值?

c - 这是在 C 中声明数组的有效方法吗?

c++ - 以 root 身份运行时出现段错误?