Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #87216

Re: Mock return_value

Newsgroups comp.lang.python
Date 2015-03-09 08:34 -0700
References <e32b0311-3b7b-4f7b-8f3b-212adae7f776@googlegroups.com>
Message-ID <4f6e3e71-4403-44e6-850a-d6d35b6a514b@googlegroups.com> (permalink)
Subject Re: Mock return_value
From Daniel <daniel.watrous@gmail.com>

Show all headers | View raw


I found that the following change worked:

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

Daniel

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?

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Mock return_value Daniel <daniel.watrous@gmail.com> - 2015-03-09 08:27 -0700
  Re: Mock return_value Daniel <daniel.watrous@gmail.com> - 2015-03-09 08:34 -0700
  Re: Mock return_value Ben Finney <ben+python@benfinney.id.au> - 2015-03-10 06:10 +1100
  Re: Mock return_value Daniel <daniel.watrous@gmail.com> - 2015-03-09 13:10 -0700

csiph-web