Mock return_value

Daniel daniel.watrous at gmail.com
Mon Mar 9 11:27:47 EDT 2015


I have a dao.py module with a dao class declared and I want to use mock to set a return value for a dao function, dao.execute_ldap_search().

import mock
import unittest
import model, dao

class TestPeopleDAO(unittest.TestCase):

    ldap_person_response = SOME_DICT

    @mock.patch('dao.dao')
    def test_find_by_last_first_comma(self, mock_dao):
        # setup the mock
        mock_dao.execute_ldap_search.return_value = self.ldap_person_response

        persondao = dao.person_dao()
        persondao.find_by_last_first_comma('name', '', '')
        self.assertEqual(len(persondao),1,"Wrong number of objects returned.")
        self.assertEqual(persondao[0].givenName, "FirstName", "Failed to parse response.")

if __name__ == '__main__':
    unittest.main()

When I run this, it fails when calling execute_ldap_search because it really calls it. I was expecting it to just return the value I specified. What am I doing wrong?



More information about the Python-list mailing list