Question

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?

Was it helpful?

Solution

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. :)

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top