Google Test - constructor declaration error

I am trying to create a test test class from a regular class with a 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: typedef unsigned int and uint8_t: typedef unsigned char

My test 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 an error:

  Error C2512: no appropriate default constructor available 

I tried to replicate the constructor built in the hello.h file to the hellotestfixture.h file. Anyway, for this? I tried to implement it in many ways, but have not yet succeeded. Any suggestions on how to implement this?

+4
source share
2 answers

After a little code correction, here is what I got for you in the store: 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(); } 

It compiles and works beautifully and hopes this answers your question. :)

+2
source

This error tells you that you are not providing the default constructor in the helloTestFixture class, which the helloTestFixture macro TEST_F to create an object of your class.

You should use part of the relationship instead of aa. Create all the hello class objects you need to test all the aspects you need.

I am not an expert at Google Test. However, looking through the documentation here:

http://code.google.com/p/googletest/wiki/Primer#Test_Fixtures:_Using_the_Same_Data_Configuration_for_Multiple_Te

http://code.google.com/p/googletest/wiki/FAQ#Should_I_use_the_constructor/destructor_of_the_test_fixture_or_t

It seems that the SetUp method is preferred. If your goal is to check the hello class, you can write it like this:

 #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 required, but it saves you the TearDown writing a TearDown function and also deletes the object in case something goes wrong.

Hope this helps.

+3
source

Source: https://habr.com/ru/post/1384141/


All Articles