为什么我不能为 zmq 编译基本的发送和接收模拟

Why can't I compile the basic send and receive mock for zmq

我正在为 socket_t::send 和 socket_t::recv 在 zmq.hpp 中编写一个 gmock(zmq c++ header only binder)

zmq_mock_class.h

#pragma once

namespace zmq {
class ZmqMockSocketClass {
public:
  virtual size_t send(const void *buf_, size_t len_, int flags_ = 0) = 0;
  virtual size_t recv (void *buf_, size_t len_, int flags_ = 0) = 0;
};

}

zmq_mock.h

#include "zmq_mock_class.h"
#include <gmock/gmock.h>
#include <zmq.hpp>

class ZmqMockSocket : public zmq::ZmqMockSocketClass {
public:
  MOCK_METHOD3(send, size_t(const void *buf_, size_t len_, int flags_));
  MOCK_METHOD3(recv, size_t(void *buf_, size_t len_, int flags_ ));
};

zmq_test.cc

using  ::testing::_;
using  ::testing::AtLeast;
TEST(ZmqSendTest, TestSendErrorAgain)
{    
   ZmqMockSocket zmq_local;
}

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

CMakeLists.txt:

cmake_minimum_required(VERSION 2.6)

# Locate GTest
find_package(GTest REQUIRED)
#find_package(GMock REQUIRED)
find_package(PkgConfig)
pkg_check_modules(GMOCK "gmock" REQUIRED)

include_directories(${GTEST_INCLUDE_DIRS})

# Link runTests with what we want to test and the GTest and pthread library
add_executable(runTests test/zmq_test.cc)
target_link_libraries(runTests ${GTEST_LIBRARIES} ${GMOCK_LIBRARIES} pthread zmq)
target_include_directories(runTests SYSTEM PRIVATE inc)

编译错误:

CMakeFiles/runTests.dir/test/zmq_test.cc.o: In function `testing::internal::FunctionMockerBase<void ()>::InvokeWith(std::tuple<> const&)':
zmq_test.cc:(.text._ZN7testing8internal18FunctionMockerBaseIFvvEE10InvokeWithERKSt5tupleIJEE[_ZN7testing8internal18FunctionMockerBaseIFvvEE10InvokeWithERKSt5tupleIJEE]+0x2e): undefined reference to `testing::internal::UntypedFunctionMockerBase::UntypedInvokeWith(void const*)'
CMakeFiles/runTests.dir/test/zmq_test.cc.o: In function `testing::internal::FunctionMockerBase<void (int)>::InvokeWith(std::tuple<int> const&)':
zmq_test.cc:(.text._ZN7testing8internal18FunctionMockerBaseIFviEE10InvokeWithERKSt5tupleIJiEE[_ZN7testing8internal18FunctionMockerBaseIFviEE10InvokeWithERKSt5tupleIJiEE]+0x2e): undefined reference to `testing::internal::UntypedFunctionMockerBase::UntypedInvokeWith(void const*)'
CMakeFiles/runTests.dir/test/zmq_test.cc.o: In function `testing::internal::FunctionMockerBase<void (int, int)>::InvokeWith(std::tuple<int, int> const&)':
zmq_test.cc:(.text._ZN7testing8internal18FunctionMockerBaseIFviiEE10InvokeWithERKSt5tupleIJiiEE[_ZN7testing8internal18FunctionMockerBaseIFviiEE10InvokeWithERKSt5tupleIJiiEE]+0x2e): undefined reference to `testing::internal::UntypedFunctionMockerBase::UntypedInvokeWith(void const*)'
CMakeFiles/runTests.dir/test/zmq_test.cc.o: In function `testing::internal::FunctionMockerBase<int ()>::InvokeWith(std::tuple<> const&)':
zmq_test.cc:(.text._ZN7testing8internal18FunctionMockerBaseIFivEE10InvokeWithERKSt5tupleIJEE[_ZN7testing8internal18FunctionMockerBaseIFivEE10InvokeWithERKSt5tupleIJEE]+0x2f): undefined reference to `testing::internal::UntypedFunctionMockerBase::UntypedInvokeWith(void const*)'
collect2: error: ld returned 1 exit status
CMakeFiles/runTests.dir/build.make:95: recipe for target 'runTests' failed
make[2]: *** [runTests] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/runTests.dir/all' failed
make[1]: *** [CMakeFiles/runTests.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

google 测试框架的构建和安装存在问题。 从 apt 包中卸载 google 测试

sudo apt remove google-mock
sudo apt-get remove googletest

从 github 下载 google 测试:

git clone https://github.com/google/googletest.git
cd googletest/

列出所有版本

$ git tag
release-1.0.0
release-1.0.1
release-1.1.0
release-1.10.0
release-1.2.0
release-1.2.1
release-1.3.0
release-1.4.0
release-1.5.0
release-1.6.0
release-1.7.0
release-1.8.0
release-1.8.1
v1.10.x

Select 正确的发行版本

git checkout release-1.8.1

构建并安装

mkdir build && cd build
cmake -DBUILD_GTEST=ON -DBUILD_GMOCK=ON -DINSTALL_GTEST=ON -DINSTALL_GMOCK=ON -D BUILD_SHARED_LIBS=ON ../
make -j8
sudo make install
sudo ldconfig -v | egrep 'gtest|gmock'

就是这样。回到测试模块构建并重建它。请注意,CMake 目前似乎没有默认的 gmock 配置。

决赛CmakeLists.txt

cmake_minimum_required(VERSION 2.6)

# Locate GTest
find_package(GTest REQUIRED)
find_package(PkgConfig)
message(STATUS "gtest found: " ${GTEST_FOUND})
pkg_check_modules(GMOCK "gmock" REQUIRED)
message(STATUS "gmock found: " ${GMOCK_FOUND})

include_directories(${GTEST_INCLUDE_DIRS})

# Link runTests with what we want to test and the GTest and pthread library
add_executable(runTests test/zmq_test.cc)
target_link_libraries(runTests ${GTEST_LIBRARIES} ${GMOCK_LIBRARIES} pthread)
target_include_directories(runTests SYSTEM PRIVATE inc)

现在有效:

$ rm -rf ./* ; cmake .. ; make
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found GTest: /usr/local/lib/libgtest.so  
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.1") 
-- gtest found: TRUE
-- Checking for module 'gmock'
--   Found gmock, version 1.9.0
-- gmock found: 1
-- Configuring done
-- Generating done
-- Build files have been written to: /home/preetam/Desktop/gmock/zmq_cpp_mock/build
Scanning dependencies of target runTests
[ 50%] Building CXX object CMakeFiles/runTests.dir/test/zmq_test.cc.o
[100%] Linking CXX executable runTests
[100%] Built target runTests

$ ./runTests 
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from ZmqSendTest
[ RUN      ] ZmqSendTest.TestSendErrorAgain
[       OK ] ZmqSendTest.TestSendErrorAgain (0 ms)
[----------] 1 test from ZmqSendTest (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (1 ms total)
[  PASSED  ] 1 test.