gmock

0

Na szybko chciałem zapytać
Instalowałem gmock i gtest przez apt-get google-mock i libgtest-dev
teraz chcąc coś skompilować:

g++ my_test.cpp -std=c++11 -lgtest -lgtest_main -lgmock -pthread

Miał ktoś już z czymś takim do czynienia?

/tmp/ccdgt5dw.o: In function `testing::internal::FunctionMockerBase<double ()>::InvokeWith(std::tuple<> const&)':
prog.cpp:(.text._ZN7testing8internal18FunctionMockerBaseIFdvEE10InvokeWithERKSt5tupleIJEE[_ZN7testing8internal18FunctionMockerBaseIFdvEE10InvokeWithERKSt5tupleIJEE]+0x20): undefined reference to `testing::internal::UntypedFunctionMockerBase::UntypedInvokeWith(void const*)'
/tmp/ccdgt5dw.o: In function `testing::internal::FunctionMockerBase<void (WeatherStation::Direction*, double*)>::InvokeWith(std::tuple<WeatherStation::Direction*, double*> const&)':
prog.cpp:(.text._ZN7testing8internal18FunctionMockerBaseIFvPN14WeatherStation9DirectionEPdEE10InvokeWithERKSt5tupleIJS4_S5_EE[_ZN7testing8internal18FunctionMockerBaseIFvPN14WeatherStation9DirectionEPdEE10InvokeWithERKSt5tupleIJS4_S5_EE]+0x1f): undefined reference to `testing::internal::UntypedFunctionMockerBase::UntypedInvokeWith(void const*)'
/tmp/ccdgt5dw.o: In function `testing::internal::FunctionMockerBase<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > (WeatherStation::Outlook)>::InvokeWith(std::tuple<WeatherStation::Outlook> const&)':
prog.cpp:(.text._ZN7testing8internal18FunctionMockerBaseIFNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEN14WeatherStation7OutlookEEE10InvokeWithERKSt5tupleIJS9_EE[_ZN7testing8internal18FunctionMockerBaseIFNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEN14WeatherStation7OutlookEEE10InvokeWithERKSt5tupleIJS9_EE]+0x24): undefined reference to `testing::internal::UntypedFunctionMockerBase::UntypedInvokeWith(void const*)'
collect2: error: ld returned 1 exit status

przy prostym takim czymś, już normalnie się kompiluje.

#include <gtest/gtest.h>
 
TEST(test, subtest) {
   ASSERT_EQ(1, 1);
}
 
int main(int argc, char **argv) {
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

problem dopiero się pojawia przy pisaniu mocków

0

No bo gmock to jest osobna biblioteka! Dodaj -lgmock -lgmock_main

W pytaniu niby wszystko jest, ale tak namieszałeś kolejność rzeczy, że trzeba się bardziej skupić, żeby załapać z czym masz problem.

0

Dodałem, nie pomaga, dalej to samo. Zamieniałem też kolejność -lgmock -lgmock_main -lgtest -lgtest_main ale ten sam błąd.

1

Ja się nie bawię w integrację z repo. Na github są templaty do gtest/gmock
https://github.com/KanoComputing/googletest-sample
Czasami są potrzebne drobne zmiany przy dostosowywaniu do kodu ale działa mi zawsze spoko.

0

Pobrałem ten sample, cmake . wypluło to:

CMake Error in tests/vendor/googletest-src/googlemock/CMakeLists.txt:
  Target "gmock_main" INTERFACE_INCLUDE_DIRECTORIES property contains path:

    "/home/9999/Pulpit/style/googletest/googletest-sample-master/tests/vendor/googletest-src/googletest/include"

  which is prefixed in the build directory.


CMake Error in tests/vendor/googletest-src/googlemock/CMakeLists.txt:
  Target "gmock_main" INTERFACE_INCLUDE_DIRECTORIES property contains path:

    "/home/9999/Pulpit/style/googletest/googletest-sample-master/tests/vendor/googletest-src/googlemock/include"

  which is prefixed in the build directory.


-- Generating done
-- Build files have been written to: /home/9999/Pulpit/style/googletest/googletest-sample-master

Po uruchomieniu `./tests/tests

[==========] Running 8 tests from 3 test suites.
[----------] Global test environment set-up.
[----------] 1 test from get_number_test
[ RUN      ] get_number_test.returns_correct_value
[       OK ] get_number_test.returns_correct_value (0 ms)
[----------] 1 test from get_number_test (0 ms total)

[----------] 1 test from ObjTest
[ RUN      ] ObjTest.test_obj_operations_with_mocks
[       OK ] ObjTest.test_obj_operations_with_mocks (0 ms)
[----------] 1 test from ObjTest (0 ms total)

[----------] 6 tests from StringsFixtureName/StringsFixture
[ RUN      ] StringsFixtureName/StringsFixture.test_string_valid/0
[       OK ] StringsFixtureName/StringsFixture.test_string_valid/0 (0 ms)
[ RUN      ] StringsFixtureName/StringsFixture.test_string_valid/1
[       OK ] StringsFixtureName/StringsFixture.test_string_valid/1 (0 ms)
[ RUN      ] StringsFixtureName/StringsFixture.test_string_valid/2
[       OK ] StringsFixtureName/StringsFixture.test_string_valid/2 (0 ms)
[ RUN      ] StringsFixtureName/StringsFixture.test_string_valid/3
[       OK ] StringsFixtureName/StringsFixture.test_string_valid/3 (0 ms)
[ RUN      ] StringsFixtureName/StringsFixture.test_string_valid/4
[       OK ] StringsFixtureName/StringsFixture.test_string_valid/4 (0 ms)
[ RUN      ] StringsFixtureName/StringsFixture.test_string_valid/5
[       OK ] StringsFixtureName/StringsFixture.test_string_valid/5 (0 ms)
[----------] 6 tests from StringsFixtureName/StringsFixture (0 ms total)

[----------] Global test environment tear-down
[==========] 8 tests from 3 test suites ran. (1 ms total)
[  PASSED  ] 8 tests.
0

W razie czego możesz poszukać czegoś innego na github z smaplami bo są. Ja z tym nie miałem problemu ale odpalałeś cmake na pewno w głównym katalogu?.
Ja mam po modyfikowaną wersję tego ale o ile pamiętam na oryginale działało z tego
https://github.com/arvidn/libtorrent/issues/3101
edit:
inne przykłady
https://github.com/davidstutz/googlemock-example.git

0

W swoim projekcie zrobiłem tak.

plik GTestAndGMock.cmake

cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR)

find_package(GTest REQUIRED)

include_directories(${GTEST_INCLUDE_DIRS})

# extrapolate gmock libs based on gtest libs data
get_filename_component(GTEST_LIB_LOCATION "${GTEST_LIBRARIES}" DIRECTORY)
get_filename_component(GTEST_LIB_EXTENTION "${GTEST_LIBRARIES}" EXT)
set(GMOCK_LIBRARIES "${GTEST_LIB_LOCATION}/libgmock${GTEST_LIB_EXTENTION}")
set(GMOCK_MAIN_LIBRARIES "${GTEST_LIB_LOCATION}/libgmock_main${GTEST_LIB_EXTENTION}")
set(GMOCK_BOTH_LIBRARIES "${GMOCK_LIBRARIES}" "${GMOCK_MAIN_LIBRARIES}")

find_package GTest jest niedorobione jeśli chodzi o gmock dlatego jest tak nakombinowanie. Odpowiednika find_package GMock nie znalazłem.
A potem, CMakeLists.txt dla testów:

include(GTestAndGMock)

add_executable(MyLibTest
    main.cpp
    test1.cpp)

target_link_libraries(MyLibTest
	${GTEST_BOTH_LIBRARIES}
	${GMOCK_BOTH_LIBRARIES}
	MyLib
)

Przy czym gmock i gtest nie są w jakikolwiek sposób śledzone przez moje repo.
Zbudowałem je ręcznie raz i zainstalowałem, żeby cmake mógł je znaleźć.
Testowane pod MacOS, bez homebrew i działa. Pod linux powinno być łatwiej bo jest porządny menadżer pakietów.

1 użytkowników online, w tym zalogowanych: 0, gości: 1