Mock the fetchone() method of the Mysql Cursor class and set its return value to None

I'm trying to make a MagicMock instance of the mysql connector, but I need for the method fetchone() to return None.

This is what I've done so far:

with mock.patch('mysql.connector.cursor') as dbmock, \
       mock.patch('mysql.connector.connect', mock.MagicMock()):
        dbcursor_mock = dbmock.return_value  # Get the mock for the cursor
        dbcursor_mock.fetchone.return_value = None  # Set the return value of fetchone

The problem is that this returns a MagicMock instance and not None.

Now, if I remove the second patch(), it does work:

with mock.patch('mysql.connector.cursor') as dbmock):
        dbcursor_mock = dbmock.return_value  
        dbcursor_mock.fetchone.return_value = None  # this does return None, Why?

But then my code will try to connect to the db and fail.

How can I make an instance of MagicMock return None?



Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation