Mock return_value
Daniel
daniel.watrous at gmail.com
Mon Mar 9 16:10:15 EDT 2015
On Monday, March 9, 2015 at 10:28:20 AM UTC-5, Daniel wrote:
> 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?
I ended up refactoring to setup patching once for a test class.
https://docs.python.org/3.5/library/unittest.mock-examples.html#applying-the-same-patch-to-every-test-method
More information about the Python-list
mailing list