0

Hello guys,

class MyClass:
    def foo(self):
        items = ["A"]

        self.bar(items)

        items.append("B")

        print(items)

    def bar(self, items):
        pass


class MyTest(unittest.TestCase):
    def test_MyClass(self):
        with patch.object(MyClass, "bar") as mock_bar:
            obj = MyClass()

            obj.foo()

            mock_bar.assert_called_once_with(["A", "B"])

This test succeeds but it shouldn't. mock_bar was called with a list of only one item, not a list of two items. I had expected the assert_called_once_with does a deep test (checking the content of the items).

Is there any way to know, when the mock / method was called, how the actual content looked like?

Thanks!

Feoggou
  • 149
  • 3
  • 11
  • http://www.voidspace.org.uk/python/mock/examples.html#coping-with-mutable-arguments – jonrsharpe Apr 21 '16 at 22:56
  • Also http://stackoverflow.com/q/16236870/3001761, did you research this at all? – jonrsharpe Apr 21 '16 at 22:57
  • Yes, I did search a bit, thought there was a problem with the assert method. The keywords I searched against on google didn't bring any useful results, I've looked on the list of suggestions when posting this question, did not find anything related (poorly chosen words, I suspect). I did search on docs.python.org/3.5/library/unittest.mock.html but it didn't say anything like that. Now I see that the "unittest.mock-examples.html" page clarifies this situation. Thanks for the help! – Feoggou Apr 23 '16 at 12:22

0 Answers0