What is wrong with my attempts to mock a simple C++ method with googlemock?


As per <a href="https://stackoverflow.com/questions/5779618/patterns-for-unit-testing-a-c-method-that-makes-a-standard-library-call" rel="nofollow">Patterns for unit testing a C++ method that makes a standard library call</a>, I'm test-driving development of a network-abstracting class. In order to unit test code that makes standard C library calls (which I can't mock) to handle BSD sockets, I've defined an interface ISocket from which both my real implementation CSocket and mock MockSocket inherit.

Now I write my first unit test for the Network class, which uses an ISocket for the heavy lifting:

#include "gmock/gmock.h" #include "gtest/gtest.h" // C standard library includes omitted #include "MockSocket.h" #include "Network.h" using ::testing::Return; namespace JrStream { class NetworkTest : public ::testing::Test { protected: Network net; ISocket * socket_ptr; virtual void SetUp() { socket_ptr = new MockSocket(); } }; TEST_F(NetworkTest, InitCallsSocket) { EXPECT_CALL((MockSocket)*socket_ptr, Socket(AF_INET, SOCK_STREAM, 0)) .Times(1) .WillOnce(Return(5)); //fake file descriptor ASSERT_TRUE(net.init(socket_ptr)); } } // namespace // gtest boilerplate main() omitted

But I get this error on compilation:

g++ -g -Wall -Lgtest-1.5.0/lib -Igmock-1.5.0/include -Igtest-1.5.0/include -I.. -I../../Debug/src -c -o NetworkTest.o NetworkTest.cc NetworkTest.cc: In member function ‘virtual void JrStream::NetworkTest_InitCallsSocket_Test::TestBody()’: NetworkTest.cc:35: error: no matching function for call to ‘JrStream::MockSocket::MockSocket(JrStream::ISocket&)’ MockSocket.h:16: note: candidates are: JrStream::MockSocket::MockSocket() MockSocket.h:16: note: JrStream::MockSocket::MockSocket(const JrStream::MockSocket&)

And here is what my interface and mock look like:

namespace JrStream { class ISocket { virtual int Socket(int domain, int type, int protocol) = 0; }; } #include "gmock/gmock.h" #include "ISocket.h" namespace JrStream { class MockSocket: public JrStream::ISocket { MOCK_METHOD3(Socket, int(int domain, int type, int protocol)); }; }

Any ideas what I'm doing wrong here?


Looks like you're invoking the copy constructor for MockSocket somewhere in TestBody without a cast.


I finally got it sorted. What was tripping me up is that I wanted to use a pointer, but googlemock works with classes. Here's what works:

#include "gmock/gmock.h" #include "gtest/gtest.h" // C standard library includes omitted #include "MockSocket.h" #include "Network.h" using ::testing::Return; namespace JrStream { class NetworkTest : public ::testing::Test { protected: Network net; MockSocket sock; }; TEST_F(NetworkTest, InitCallsSocket) { EXPECT_CALL(sock, Socket(AF_INET, SOCK_STREAM, 0)) .Times(1) .WillOnce(Return(5)); //fake file descriptor ASSERT_TRUE(net.init(&sock)); } } // namespace // gtest boilerplate main() omitted

This is well and good, but I wonder how to make this work if I really needed my pointers. Sounds like an exercise for the reader. ;)


How about:

TEST_F(NetworkTest, InitCallsSocket) { EXPECT_CALL(*((MockSocket)*socket_ptr), Socket(AF_INET, SOCK_STREAM, 0)) .Times(1) .WillOnce(Return(5)); //fake file descriptor ASSERT_TRUE(net.init(socket_ptr));


(Disclaimer - I did not check if it compiles...)


  • What is wrong with my attempts to mock a simple C++ method with googlemock?
  • Sidekiq on heroku - Error R14 (Memory quota exceeded)
  • Building a dynamic dropdown in javascript
  • SQL Server : login success but “The database [dbName] is not accessible. (ObjectExplorer)”
  • Why is it ElasticSearch is not allowed to run as root?
  • If a computer can be Turing complete with one instruction what is the purpose of having many instruc
  • Hierarchical (Multi-column) Sorting for the .net GridView?
  • Can Django use “external” python scripts linked to other libraries (NumPy, RPy2…)
  • Why is parser combinator “seq” defined with “bind” and “return”?
  • python pandas- AttributeError: 'Series' object has no attribute 'columns'?
  • State management in ReactJS
  • Matplotlib change colormap tab20 to have three colors
  • C++ Coin flip program error
  • string.IsNullOrEmpty() Doesn't Seem to Work on a String within a Class within a Class
  • WPF Template Binding in ToggleButton UserControl
  • Laravel: Getting Session ID oddly truncates when using foreach
  • Bypass multiple inheritance in Java
  • Recording logins for password protected directories
  • how to do an event when i swipe from fragment to the other
  • Display issues when we change from one jquery mobile page to another in firefox
  • Deselecting radio buttons while keeping the View Model in synch
  • JSON with duplicate key names losing information when parsed
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Jquery - Jquery Wysiwyg return html as a string
  • Buffer size for converting unsigned long to string
  • Hits per day in Google Big Query
  • JTable with a ScrollPane misbehaving
  • How to get Windows thread pool to call class member function?
  • unknown Exception android
  • Checking variable from a different class in C#
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • failed to connect to specific WiFi in android programmatically
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • How can I use threading to 'tick' a timer to be accessed by other threads?