• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • About
  • Life
  • Tech
  • Travel
  • Work
  • Questions
  • Contact

Welcome

.

How to write a gmock test such that it does not call constructor of the fixture class

April 10, 2020 by

Questions › How to write a gmock test such that it does not call constructor of the fixture class
0
Vote Up
Vote Down
Garmaine asked 4 years ago

I would like to know if there is anyway I can write a gmock unit test in C++ that does not call the constructor of the existing fixture class.

My question is specific to existing code hence first I will summarize how existing code is structured.

In the C++ file, all the existing unit tests use the fixture. The constructor of that fixture builds configuration needed to execute all those unit tests.

The code in that file looks something like this.

class TestHandlerFixture : public HandlerFixture
{
public:
    TestHandlerFixture()
    {
        // Lot of code here including below call
        HandlerImplemantaion::create_link(Arg1);
    }
    ~TestHandlerFixture()
    {
        // Code to clean up
    }
}


BOOST_FIXTURE_TEST_SUITE(TestHandler, TestHandlerFixture);

BOOST_AUTO_TEST_CASE(test_one)
{

}

BOOST_AUTO_TEST_CASE(test_two)
{

}

// Lot more unit tests

BOOST_AUTO_TEST_SUITE_END();

I modified code that is called by the create_link() function and hence I want to add a new unit test to test the new code. The new code that I added is an error case and is not applicable to the regular flow of the create_link(). In the new test, I want to set error condition first and then assert using EXPECT_CALL(…) when create_link() is called. Any idea how to do that without affecting the existing test cases or significantly modifying the existing unit tests?

Since create_link() is called inside the constructor and hence it executes for every test, I don't think I can set error condition inside the constructor since it will affect all the existing tests. I can delete create_link() call from the constructor and then explicitly call it in every unit test but that than I have to make changes in lot of places. I would like to know if there is any other way I can write unit test without significantly impacting existing tests. Is there way I can write unit test that does not call the constructor of the TestHandlerFixture class?

My idea about new unit test is something like this.

BOOST_AUTO_TEST_CASE(test_create_link_error)
{
    // Code here to set error condition
    EXPECT_CALL(...); // This check for error when create_link() is called.
    create_link(Arg1);
}

The above code does not work since TestHandlerFixture() constructor gets called first and hence it calls create_link(). Thanks.

Are you looking for the answer?
Original Question and Possible Answers can be found on `http://stackoverflow.com`

Question Tags: boost-test, c++, gmock, unit-testing

Please login or Register to submit your answer




Primary Sidebar

Tags

Advancements best Business strategies commercial convenience economic Finances Cognitive decline Financial growth firm Future Hidden Gems Home hydration Impact Innovations lighting line of work Mental health Must-See New York City office patronage Productivity profession Profitability tips Profit optimization pursuit recreation Revenue enhancement romance sippy cups social station Technological breakthroughs technology toddlers trading transaction Treasures Uncover undertaking Well-being Wonders Work Young onset dementia

Newsletter

Complete the form below, and we'll send you all the latest news.

Footer

Footer Funnies

Who knew that reading the footer could be such a hilarious adventure? As we navigate websites, books, and documents, we often stumble upon the unassuming space at the bottom, only to discover a treasure trove of amusement. In this side-splitting compilation, we present 100 jokes that celebrate the unsung hero of content – the footer. Get ready to chuckle, giggle, and maybe even snort as we dive into the world of footnotes, disclaimers, and hidden comedic gems. Brace yourself for a wild ride through the footer!

Recent

  • Unveiling the Enigma: Almost-Magical Lamp Lights Highway Turns
  • The Impact of Young Onset Dementia on Employment and Finances: Optimizing Post-Diagnostic Approaches
  • 11 Wonders of 2023 Technological Breakthrough – Unveiling the Future
  • Work from Home and Stay Mentally Sane – Achieve Productivity and Well-being
  • Hidden Gems of New York City – Uncover the Must-See Treasures!

Search

Tags

Advancements best Business strategies commercial convenience economic Finances Cognitive decline Financial growth firm Future Hidden Gems Home hydration Impact Innovations lighting line of work Mental health Must-See New York City office patronage Productivity profession Profitability tips Profit optimization pursuit recreation Revenue enhancement romance sippy cups social station Technological breakthroughs technology toddlers trading transaction Treasures Uncover undertaking Well-being Wonders Work Young onset dementia

Copyright © 2023