Complete the code to create a mock object using unittest.mock.
from unittest.mock import [1] mock_obj = [1]()
The Mock class is used to create a basic mock object in unittest.mock.
Complete the code to assert that a mock method was called exactly once.
mock_obj.method()
mock_obj.method.[1]()assert_called_once() checks that the mock method was called exactly one time.
Fix the error in the code to set a return value for a mock method.
mock_obj = MagicMock() mock_obj.get_data.[1] = 42
The return_value attribute sets what the mock method returns when called.
Fill both blanks to patch a function and check it was called with specific arguments.
from unittest.mock import patch with patch('module.func') as mock_func: mock_func([1]) mock_func.[2]('hello', 5)
The patched function is called with arguments 'hello' and 5, then assert_called_with checks those exact arguments.
Fill all three blanks to create a MagicMock, set a side effect, and verify the exception is raised.
from unittest.mock import MagicMock import pytest mock_func = MagicMock() mock_func.side_effect = [1] with pytest.raises([2]): mock_func([3])
The side_effect is set to a lambda that raises ValueError. The test checks that calling mock_func(42) raises ValueError.