jeudi 5 février 2015

How to mock.patch MySQLdb.cursors?


class CanonDatabase:
def __init__(self, clean_up_db=False):
self.db = "tmg_canon_april_tokens"
self.conn = create_connection(self.db)
self.cur = self.conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)


I am trying to mock.patch MySQLdb in order to pass the constructor.



@mock.patch.object(MySQLdb, '__init__')
class TestDatabase(unittest.TestCase):
def setUp(self):
super(TestDatabase, self).setUp()
patch = mock.patch('atone_canon.Database.create_connection')
mock_baz = patch.start()
mock_baz.cursor.return_value = mock.MagicMock()

def tearDown(self):
super(TestDatabase, self).tearDown()
mock.patch.stopall()

def test_clean_table(self, mysql_mock):
db = CanonDatabase()
self.assertEqual(True, False)


However this fails with the following error message:




File "atone_canon/Database.py", line 20, in __init__ self.cur = self.conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)


AttributeError: 'module' object has no attribute 'cursors'



Aucun commentaire:

Enregistrer un commentaire