I\'ve been pulling my hair out trying to figure out how to mock the sqlite3.Cursor
class specifically the fetchall
method.
Consider the followi
I would take the approach of patching out sqlite3 imported in your module and then work from there.
Let's assume your module is named what.py
.
I would patch out what.sqlite3
and then mock the return value of .connect().cursor().fetchall
.
Here is a more complete example:
from mock import patch
from nose.tools import assert_true, assert_false
from what import Foo
def test_existing_name():
with patch('what.sqlite3') as mocksql:
mocksql.connect().cursor().fetchall.return_value = ['John', 'Bob']
foo = Foo()
assert_true(foo.check_name('John'))