Reputation: 16992
In my source class I have the following statements
def main():
conn = create_connection()
cur = conn.cursor()
cur.execute(query)
In my test class , I am trying to assert the the "query" passed to cur.execute method. But I would like to mock both create_connection and the cursor object returned by cursor() function. I was able to mock create_connection using the code below. How can I patch cursor and assert the value of query passed to execute function?
@patch(main.create_connection)
def test_main(self,mock_conn):
Upvotes: 0
Views: 234
Reputation: 16815
As the created cursor is already a mock (e.g. each call to a mock object creates another mock object), you don't have to configure anything else. You can just get the cursor mock:
@patch('main.create_connection')
def test_main(mock_conn):
cursor = mock_conn.return_value.cursor.return_value
main()
cursor.execute.assert_called_once_with(query)
Upvotes: 1