mardi 12 février 2019

Gtest: mocking free function in a constructor

I read a lot of documentation related to Gtest mocking (e.g., https://github.com/google/googletest/blob/master/googlemock/docs/CookBook.md, "Mocking Free Functions"), but couldn't find the solution for the following problem:

source.cpp

H::H()
{
    // some code1
    if (to_be_mocked(id) != 0) { // some code2 }
    // some code3
}

H& H::get_instance()
{
    static H s;
    return s;
}

unit_test.cpp

#include "gtest/gtest.h"
#include "gmock/gmock.h"

#include "source.h"

TEST(Source, Constructor)
{
    // What to write here to mock function "to_be_mocked"?
    H& inst = H::get_instance();
}

int main(int argc, char** argv)
{
  testing::InitGoogleMock(&argc, argv);
  return RUN_ALL_TESTS();
}

So, I need to test the whole code in the H's constructor and mock function to_be_mocked which is defined in a different translation unit. How can I do it from unit_test.cpp?

Aucun commentaire:

Enregistrer un commentaire