title: Python Mock object with method called multiple times
body: <p>I have a class that I'm testing which has as a dependency another class (an instance of which gets passed to the CUT's init method).  I want to mock out this class using the Python Mock library.</p>
  
  <p>What I have is something like:</p>
  
  <pre><code>mockobj = Mock(spec=MyDependencyClass)
  mockobj.methodfromdepclass.return_value = "the value I want the mock to return"
  assertTrue(mockobj.methodfromdepclass(42), "the value I want the mock to return")
  
  cutobj = ClassUnderTest(mockobj)
  </code></pre>
  
  <p>Which is fine, but "methodfromdepclass" is a parameterized method, and as such I want to create a single mock object where depending on what arguments are passed to methodfromdepclass it returns different values.  </p>
  
  <p>The reason I want this parameterized behaviour is I want to create multiple instances of ClassUnderTest that contain different values (the values of which are produced by what gets returned from the mockobj).</p>
  
  <p>Kinda what I'm thinking (this of course does not work):</p>
  
  <pre><code>mockobj = Mock(spec=MyDependencyClass)
  mockobj.methodfromdepclass.ifcalledwith(42).return_value = "you called me with arg 42"
  mockobj.methodfromdepclass.ifcalledwith(99).return_value = "you called me with arg 99"
  
  assertTrue(mockobj.methodfromdepclass(42), "you called me with arg 42")
  assertTrue(mockobj.methodfromdepclass(99), "you called me with arg 99")
  
  cutinst1 = ClassUnderTest(mockobj, 42)
  cutinst2 = ClassUnderTest(mockobj, 99)
  
  # now cutinst1 &amp; cutinst2 contain different values
  </code></pre>
  
  <p>How do I achieve this "ifcalledwith" kind of semantics?</p>