title: Python Mock UnitTest and Database
body: <p>I want to stub out a database call when I test a method I have in my code. All I want it to do is return me values but I cannot seem to get that far.</p>
  
  <pre class="lang-py prettyprint-override"><code>def loadSummary(appModel):
      stmt = 'Select * from Table'
      for row in appModel.session.query(*t.columnNames()).from_statement(stmt).all():
          t.append(row)
      return t
  
  def test_loadSummary(self):
      appModel = Mock()
      query = appModel.session.query.return_value
      query.from_statment.return_value = ['test1', 'test2']
      expected = loadSummary(appModel)
  </code></pre>
  
  <p>I get the following error</p>
  
  <pre class="lang-py prettyprint-override"><code>for row in appModel.session.query(*t.columnNames()).from_statement(stmt).all():
  TypeError: 'Mock' object is not iterable
  </code></pre>
  
  <p>So its like its not getting passed into the method at all even though it works in the shell no problem.</p>
  
  <pre class="lang-py prettyprint-override"><code>&gt;&gt;&gt; appModel.session.query('').from_statment('stmt')
  ['test1', 'test2']
  </code></pre>
  
  <p>I then tried using mock.patch.object</p>
  
  <pre class="lang-py prettyprint-override"><code>class MockAppContoller(object):
      def from_from_statement(self, stmt):
          return ['test1', 'test2']
  
  def test_loadSummary(self):
      with mock.patch.object(loadSummary, 'appModel') as mock_appModel:
          mock_appModel.return_value = MockAppContoller()
  </code></pre>
  
  <p>I get the following error
  </p>
  
  <pre><code>2014-04-09 13:20:53,276 root ERROR Code failed with error:
  &lt;function loadSummary at 0x0D814AF0&gt; does not have the attribute 'appModel'
  </code></pre>
  
  <p>How can I get around this problem?</p>