title: mock/patch os.path.exists with multiple return values
body: <p>I'm trying to test a function that I made that iterates through a list, and calls <code>os.path.exists</code> for each item in the list. My test is passing the function a list of 2 objects. I need <code>os.path.exists</code> to return <code>True</code> for one of them and <code>False</code> for the other. I have tried this:</p>
  
  <pre><code>import mock
  import os
  import unittest
  
  class TestClass(unittest.TestCase):
      values = {1 : True, 2 : False}
      def side_effect(arg):
          return values[arg]
  
      def testFunction(self):
          with mock.patch('os.path.exists') as m:
              m.return_value = side_effect # 1
              m.side_effect = side_effect # 2
  
              arglist = [1, 2]
              ret = test(argList)
  </code></pre>
  
  <p>Using either but not both of line #1 and #2 give <code>NameError: global name 'side_effect' is not defined</code></p>
  
  <p>I found this <a href="https://stackoverflow.com/q/7665682/1393498">question</a> and modified my code like so:</p>
  
  <pre><code>import mock
  import os
  
  class TestClass(unittest.TestCase):
      values = {1 : True, 2 : False}
      def side_effect(arg):
          return values[arg]
  
      def testFunction(self):
          mockobj = mock(spec=os.path.exists)
          mockobj.side_effect = side_effect
  
          arglist = [1, 2]
          ret = test(argList)
  </code></pre>
  
  <p>And this produces <code>TypeError: 'module' object is not callable</code>.
  I also tried switching these lines:</p>
  
  <pre><code>mockobj = mock(spec=os.path.exists)
  mockobj.side_effect = side_effect
  </code></pre>
  
  <p>for this</p>
  
  <pre><code>mockobj = mock(spec=os.path)
  mockobj.exists.side_effect = side_effect
  </code></pre>
  
  <p>and this</p>
  
  <pre><code>mockobj = mock(spec=os)
  mockobj.path.exists.side_effect = side_effect
  </code></pre>
  
  <p>with the same error being produced. Can anyone point out what it is that I am doing wrong and what I can do to get this to work?</p>
  
  <p><strong>EDIT:</strong>
  After posting my answer below I realised that my first bit of code actually works as well, I just needed <code>m.side_effect = TestClass.side_effect</code> instead of <code>m.side_effect = side_effect</code>.</p>