title: How to set up a mock server with GoogleMock
body: <p>I am converting old C code to C++, and I am testing it as I rewrite with GoogleTest. Part of my code uses sockets to connect to a remote host, with an IPV4 address. I am having a lot of trouble understanding how I can use GoogleTest and GoogleMock to test the socket part of my code.</p>
  
  <p>My object takes in a host IP address and number of seconds to wait before trying to connect to a host again. There is a function called "conn()" that actually tries to connect to the host. That is the function I want to test. So in my test, I want to do something like: </p>
  
  <pre><code>TEST(CONNECTION_TEST, Connection) 
  {
      MyObject obj("###.###.###.###", 1);
      ASSERT_TRUE(obj.conn());
  }
  </code></pre>
  
  <p>I read on here that I needed to mock up a server since testing a remote host isn't really advisable. I am brand new to GoogleMock, so I read the official documentation and read the <a href="https://github.com/google/googletest/blob/master/googletest/docs/primer.md" rel="nofollow noreferrer">GoogleTest primer</a>, <a href="https://github.com/google/googletest/blob/master/googlemock/docs/ForDummies.md" rel="nofollow noreferrer">GoogleMock For Dummies</a>, the <a href="https://github.com/google/googletest/blob/master/googlemock/docs/CheatSheet.md" rel="nofollow noreferrer">GoogleMock Cheat Sheet</a>, and the <a href="https://github.com/google/googletest/blob/master/googlemock/docs/CookBook.md" rel="nofollow noreferrer">GoogleMock Cookbook</a>. I watched CodesBay's <a href="https://www.youtube.com/watch?v=dLB2aDasVTg" rel="nofollow noreferrer">tutorial</a>. I looked at several posts on SO, including <a href="https://stackoverflow.com/questions/5780747/what-is-wrong-with-my-attempts-to-mock-a-simple-c-method-with-googlemock">What is wrong with my attempts to mock a simple C++ method with googlemock?</a>, <a href="https://stackoverflow.com/questions/5779618/patterns-for-unit-testing-a-c-method-that-makes-a-standard-library-call">Patterns for unit testing a C++ method that makes a standard library call</a>, and <a href="https://stackoverflow.com/questions/41753538/how-to-unit-test-bsd-sockets">How to unit test BSD sockets</a>.</p>
  
  <p>For my mock server, I did the following. I made a Socket class that wraps socket.h, and then I extended MockSocket from it. In my TEST, I set up the mock socket to accept connections, like so:</p>
  
  <pre><code>TEST(CONNECTION_TEST, Connection)
  {
  
      // bind to local host?
      MyObject obj("0.0.0.0", 1);
  
      // create the host
      MockSocket mock_sock;
      MockSocket new_sock;
      EXPECT_CALL(mock_sock, create()).Times(1);
      EXPECT_CALL(mock_sock, bind(_));
      EXPECT_CALL(mock_sock, listen());
      EXPECT_CALL(mock_sock, accept(_));
      mock_sock.create();
      mock_sock.bind(123);
      mock_sock.listen();
      mock_sock.accept(new_sock);
  
      ASSERT_TRUE(obj.conn());
  }
  </code></pre>
  
  <p>As you can tell, I don't know what I am doing. My test results fail and I think it is because I just didn't do this correctly. Please, can someone guide me in the right direction? Is the mock server the correct way to test my object?</p>