python - Replacing an object with mocks -


i'm not sure i'm doing wrong. perhaps have wrong end of stick mocking. assumption when use mocks magic , replaces objects in original code.

sites.py

class sites:     def __init__(self):         pass      def get_sites(self):         return ['washington', 'new york'] 

my_module.py

from mylib import sites  def get_messages():     # sites     sm = sites.sites()     sites = sm.get_sites()     print('sites:' , sites)      site in sites:         print('test: ' , site) 

my_test.py

import my_module import unittest unittest.mock import patch   class mymoduletestcase(unittest.testcase):      @patch('my_module.sites')     def test_process_the_queue(self, mock_sites):         mock_sites.get_sites.return_value = ['london', 'york']          print(mock_sites.get_sites())         my_module.get_messages()  if __name__ == '__main__':     unittest.main() 

running following output:

.['london', 'york'] sites: <magicmock name='sites().get_sites()' id='139788231189504'>  ---------------------------------------------------------------------- ran 1 test in 0.002s  ok [finished in 0.1s] 

i expecting second print output (which occurs within my_module.py) same first , loop through list passed through return value.

any appreciated.

updated show how importing class

python mock, while silly powerful, not intuitive use.

the print statement shows patching my_module.sites correctly have not registered get_sites return value correctly, , should be:

mock_sites.return_value.get_sites.return_value = ['london', 'york']

the print statement shows there call sites().get_sites() registered on patched object:

sites: <magicmock name='sites().get_sites()' id='139788231189504'>

when reading find helpful translate () return_value

sites.return_value.get_sites.return_value

the return value missing represents instantiation of mock sites object: sites().


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -