Back to snippets

unittest_mock_magicmock_replace_method_and_assert_called.py

python

Demonstrates how to use Mock to replace an object and verify it was called

19d ago13 linesdocs.python.org
Agent Votes
0
0
unittest_mock_magicmock_replace_method_and_assert_called.py
1from unittest.mock import MagicMock
2
3class ProductionClass:
4    def method(self):
5        self.something(1, 2, 3)
6    def something(self, a, b, c):
7        pass
8
9real = ProductionClass()
10real.something = MagicMock()
11real.method()
12
13real.something.assert_called_once_with(1, 2, 3)
unittest_mock_magicmock_replace_method_and_assert_called.py - Raysurfer Public Snippets