title: mocking a socket connection in Python
body: <p>I am trying to write unit tests for a class in python. The class opens a tcp socket on <strong>init</strong>. I am trying to mock this out so that I can assert that connecting is called with the correct values but obviously doesn't actually happen in unit tests. I have tired MagicMock, patch, etc but I have not found a solution. </p>
  
  <p>My class so far looks like this</p>
  
  <pre><code>import socket
  
  class MyClass(object):
  
      def __init__(self):
          self.tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
          self.tcp_socket.connect('0.0.0.0', '6767')
  </code></pre>