python how to access input params in MagicMock?
Question: I want to add a unit test for the function 'method_a':

    
    
    def method_a(some_thing):
        #some logic here
        return update({'a':1}, request=some_thing)
    
    def update(value, request):
        if request:
          return value.update({'b':2})
        return value.update({'c':3})
    

Actually I don't care the logic of update method. All I want to do is to use
MagicMock or whatever tools to mock update method, and let it return the first
paramater of it received (In this case, should return {'a': 1}). How can I do
that?


Answer: You can use `patch` function. Here's an example:

    
    
    from unittest import TestCase
    from mock import patch
    
    
    def method_a(some_thing):
        #some logic here
        return update({'a': 1}, request=some_thing)
    
    
    def update(value, request):
        if request:
            return value.update({'b': 2})
        return value.update({'c': 3})
    
    
    class TestMethodA(TestCase):
        def test_mocked_update(self):
            with patch('%s.update' % __name__, lambda value, request: value):
                self.assertEqual(method_a('test'), {'a': 1})
    

Hope that helps.

