call_args_list: several calls with same object as argument, but different content in object
See original GitHub issueWhat steps will reproduce the problem?
1. See code below
What is the expected output? What do you see instead?
I have a function that calls another function several times. The second
function takes a dictionary as argument, and the first function manipulates
this dictionary for each time it calls the second function.
I want to be able to check how the dictionary looked like at the time of each
call to the second function. I tried using call_args_list, but found it to
return the last value for all calls.
I understand the nature of pointers, and that the call_args_list only point to
the dictionary, which has changed when I do the check. But this limits the use
of call_args_list a lot. Is there some other way to do this? If not, is it
possible to implement in a future version of Mock?
What version of the product are you using? On what operating system?
Mock 0.8, Ubuntu 12.04
Please provide any additional information below.
Thanks for an otherwise great lib :)
#####code_to_test.py#################
class ParameterClass(object):
def __init__(self, value):
self.value = value
class ClassToTest(object):
def function_to_test(self, parameters):
lst = parameters['key'].value.split('-')
for val in lst:
parameters['key'].value = val
print 'Calling with value %s' % parameters['key'].value
self.function_to_be_mocked(parameters)
def function_to_be_mocked(self, parameters):
print 'Called with value %s' % parameters['key'].value
if __name__ == '__main__':
parameters = {'key': ParameterClass('value1-value2')}
cls = ClassToTest()
cls.function_to_test(parameters)
#######test.py####################
import unittest2 as unittest
from mock import Mock
from code_to_test import ClassToTest
class SomeTest(unittest.TestCase):
def test_something(self):
cls = ClassToTest()
cls.function_to_be_mocked = Mock()
parameter_mock = Mock()
parameter_mock.value = 'value1-value2'
parameters = {'key': parameter_mock}
cls.function_to_test(parameters)
print cls.function_to_be_mocked.call_args_list[0][0][0].value
print cls.function_to_be_mocked.call_args_list[1][0][0].value
if __name__ == '__main__':
unittest.main()
Original issue reported on code.google.com by erling.b...@gmail.com on 11 Sep 2012 at 8:22
Issue Analytics
- State:
- Created 8 years ago
- Comments:6
Top Results From Across the Web
How do we ensure that the calls in the Mock.call_args_list ...
call_args_list contain the states of all objects at the time of calling the mock rather than at the time of checking the arguments...
Read more >unittest.mock — mock object library — Python 3.11.1 ...
The function is called with the same arguments as the mock, and unless it returns DEFAULT , the return value of this function...
Read more >26.6. unittest.mock — getting started
Another common use case is to pass an object into a method (or some part of the system ... The function will be...
Read more >Further Examples — Mock 1.0.1 documentation
This means that you can see how the object returned from a call to a mocked object ... A chained call is several...
Read more >Python's unittest.mock | seanh.cc
Your test will need a Bar object to pass foo() , but there can be ... uses the Bar object - how many...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

+1
@serpulga - This repo is only for issues that are specific to the backport. Please report issues with mock in the upstream bug tracker at https://bugs.python.org/. Once issues are fixed upstream, they can be backported here.