title: Mocking chained calls in Python
body: <p>I am trying to test the following class using <code>unittest</code> and the <code>mock</code> library:</p>
  
  <pre><code>class Connection(object):
      def __init__(self, cookie):
      self.connect = None
      self.session = Session()
      self.session.load(cookie)
      # do some stuff with self.session
      self.some_info = self.session.data['the_info']
  </code></pre>
  
  <p>How could I test if when I create an instance of <code>Connection</code>, depending on the return of the <code>Session</code> instance, I assert if <code>self.some_info</code> is with the value I am expecting?</p>
  
  <p>I wish to use the mock library. In its documentation I have an example of mocking chained calls (<a href="http://www.voidspace.org.uk/python/mock/examples.html#mocking-chained-calls" rel="nofollow">http://www.voidspace.org.uk/python/mock/examples.html#mocking-chained-calls</a>), but it isn't very clear of how I can adapt it to my problem.</p>
  
  <p>The <code>Session.load(cookie)</code> method sets some attributes in the <code>Session</code> instance. I would like to set this values fixed for my tests for every value of cookie.</p>