title: How to mock <ModelClass>.query.filter_by() in Flask-SqlAlchemy
body: <h2>In brief</h2>
  
  <p>When testing a model class in Flask-SqlAlchemy, how can we mock the method <code>.query.filter_by()</code> so as to return the list of mocked model objects?</p>
  
  <h2>Full details</h2>
  
  <p>Let's say we have a model class as below code</p>
  
  <pre><code>from flask.ext.sqlalchemy import SQLAlchemy
  db = SQLAlchemy()
  class SomeModel(db.Model):
      # more column mapping and methods go here
  </code></pre>
  
  <p>Then in our Flask code we call</p>
  
  <pre><code>SomeModel.query.filter_by(...)
  </code></pre>
  
  <p>In our testing code, using <a href="https://docs.python.org/2/library/unittest.html" rel="noreferrer">Python unittest</a> model with <a href="https://www.toptal.com/python/an-introduction-to-mocking-in-python" rel="noreferrer">mocking</a>, we want to mock the <code>filter_by()</code> call so that it returns a list of model objects under our designed test case.</p>
  
  <p>How can we get to that?</p>
  
  <p>p.s. </p>
  
  <p>My google search only found <a href="https://stackoverflow.com/a/28194452/248616">this related post</a>; though applying <code>@patch("flask_sqlalchemy.SignallingSession", autospec=True)</code> at the beginning of the class not work for me.</p>
  
  <p>I also tried to mock the function as below code snippet</p>
  
  <pre><code>@patch('app.model.some_model.SomeModel.query.filter_by')
  def test_some_case(self, filterbyMOCK):
      # more test logic goes here
  </code></pre>
  
  <p>and the code get immediate error when started</p>
  
  <pre><code>RuntimeError: application not registered on db instance and no application bound to current context
  </code></pre>
  
  <p>The full error from PyCharm IDE as snapshot below.</p>
  
  <pre><code>Traceback (most recent call last):
    File "/home/namgivu/NN/code/someproject-cloud/venv/local/lib/python2.7/site-packages/mock/mock.py", line 1297, in patched
      arg = patching.__enter__()
    File "/home/namgivu/NN/code/someproject-cloud/venv/local/lib/python2.7/site-packages/mock/mock.py", line 1353, in __enter__
      self.target = self.getter()
    File "/home/namgivu/NN/code/someproject-cloud/venv/local/lib/python2.7/site-packages/mock/mock.py", line 1523, in &lt;lambda&gt;
      getter = lambda: _importer(target)
    File "/home/namgivu/NN/code/someproject-cloud/venv/local/lib/python2.7/site-packages/mock/mock.py", line 1210, in _importer
      thing = _dot_lookup(thing, comp, import_path)
    File "/home/namgivu/NN/code/someproject-cloud/venv/local/lib/python2.7/site-packages/mock/mock.py", line 1197, in _dot_lookup
      return getattr(thing, comp)
    File "/home/namgivu/NN/code/someproject-cloud/venv/local/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 428, in __get__
      return type.query_class(mapper, session=self.sa.session())
    File "/home/namgivu/NN/code/someproject-cloud/venv/local/lib/python2.7/site-packages/sqlalchemy/orm/scoping.py", line 78, in __call__
      return self.registry()
    File "/home/namgivu/NN/code/someproject-cloud/venv/local/lib/python2.7/site-packages/sqlalchemy/util/_collections.py", line 990, in __call__
      return self.registry.setdefault(key, self.createfunc())
    File "/home/namgivu/NN/code/someproject-cloud/venv/local/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 136, in __init__
      self.app = db.get_app()
    File "/home/namgivu/NN/code/someproject-cloud/venv/local/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 809, in get_app
      raise RuntimeError('application not registered on db '
  RuntimeError: application not registered on db instance and no application bound to current context
  </code></pre>