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

发布于 2024-12-19 04:23:48 字数 1016 浏览 0 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静赏你的温柔 2024-12-26 04:23:48

此错误告诉您,您没有在 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 类,您可以这样编写:

#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 函数,如果出现问题,它也会删除该对象。

希望这有帮助。

This error is telling you that you are not providing a default constructor in the helloTestFixture class, needed by the TEST_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 class hello, you could write it this way:

#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 is not really needed, but it will save you the effort of writing the TearDown function, and it also will delete the object in case something goes wrong.

Hope this helps.

一桥轻雨一伞开 2024-12-26 04:23:48

经过不多的代码修正后,以下是我为您准备的内容:答案:)

class hello
{
public:
  hello(const uint32_t argID, const uint8_t argCommand);
virtual ~hello();
void initialize();
};

hello::hello(const uint32_t argID, const uint8_t argCommand){/* do nothing*/}
hello::~hello(){/* do nothing*/}
void hello::initialize(){/* do nothing*/}

class helloTestFixture
{
public:
  helloTestFixture();
  virtual ~helloTestFixture();
  hello m_object;
};

helloTestFixture::helloTestFixture():m_object(0,0){/* do nothing */}
helloTestFixture::~helloTestFixture(){/* do nothing */}

int main()
{
    helloTestFixture htf;
    htf.m_object.initialize();
}

它可以很好地编译和运行,希望这能回答您的问题。 :)

After not much of code correction, here's what I've got for you in store: An Answer :)

class hello
{
public:
  hello(const uint32_t argID, const uint8_t argCommand);
virtual ~hello();
void initialize();
};

hello::hello(const uint32_t argID, const uint8_t argCommand){/* do nothing*/}
hello::~hello(){/* do nothing*/}
void hello::initialize(){/* do nothing*/}

class helloTestFixture
{
public:
  helloTestFixture();
  virtual ~helloTestFixture();
  hello m_object;
};

helloTestFixture::helloTestFixture():m_object(0,0){/* do nothing */}
helloTestFixture::~helloTestFixture(){/* do nothing */}

int main()
{
    helloTestFixture htf;
    htf.m_object.initialize();
}

This compiles and runs nicely and hope this answers your question. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文