
#This howto is not complete as there usually are more requiremets. It's usually easier to start with a test that already works and copy that one.


Mocking QgsProjectInstance:
#----------------------------
#Add a tuple with the requested values from definitions.midvatten_defs.settingsdict as key and the return value as value
MIDV_DICT = lambda x, y: {('Midvatten', 'database'): [TEMP_DB_PATH], ('Midvatten', 'locale'): ['sv_SE']}[(x, y)]

class ATestClass():
    @mock.patch('midvatten_utils.QgsProject.instance')
    def atest(self, mock_qgsproject_instance):
        mock_qgsproject_instance.return_value.readEntry = MIDV_DICT


#Mock answers from import_data_to_db.utils.Askuser:
#----------------------------
@mock.patch('import_data_to_db.utils.Askuser')
def x(mock_askuser):
    def side_effect(*args, **kwargs):
        mock_result = mock.MagicMock()
        if args[1].startswith('Do you want to confirm'):
            mock_result.result = 0
            return mock_result
            #mock_askuser.return_value.result.return_value = 0
        elif args[1].startswith('Do you want to import all'):
            mock_result.result = 0
            return mock_result
        elif args[1].startswith('Please note!\nForeign keys'):
            mock_result.result = 1
            return mock_result
        elif args[1].startswith('Please note!\nThere are'):
            mock_result.result = 1
            return mock_result
        elif args[1].startswith('It is a strong recommendation'):
            mock_result.result = 0
            return mock_result
    mock_askuser.side_effect = side_effect

#Using mock expected:

(From mocks own tests)
from mock import (
    call, create_autospec, MagicMock,
    Mock, ANY, patch, PropertyMock
)

expected = [
    call(ANY, foo=ANY, bar=ANY),
    call.method(ANY, zinga=ANY, alpha=ANY),
    call(), call().method(a1=ANY, z99=ANY)
]
self.assertEqual(expected, mock.mock_calls)

#This can be shortened into:
#print(str(mock_iface.mock_calls)) #Copy the relevant calls from the print result.

assert call.messageBar().createMessage('Warning, In total 2 posts were not imported.') in mock_iface.mock_calls
assert call.messageBar().createMessage('In total 1 measurements were imported to "obs_points".') in mock_iface.mock_calls


#Mock multiple answers from NotFoundQuestion
mocks_notfoundquestion = []
for answer, value, reuse_column in [['ok', 'rb1', 'location'],
                      ['ok', 'rb2', 'location'],
                      ['skip', 'rb3', 'location']]:
    a_mock = MagicMock()
    a_mock.answer = answer
    a_mock.value = value
    a_mock.reuse_column = reuse_column
    mocks_notfoundquestion.append(a_mock)

mock_notfoundquestion.side_effect = mocks_notfoundquestion

#print("\nRef\n" + reference_string + "\ntest\n" + test_string)

Postgis tests require both of these:
