The Case:
I've a class View
in my code that creates an instance during execution, which one I want to mock.
I'm passing implementation of View
before running the code.
class View:
def __init__(arg):
self.arg = arg
self.inner_class = self.InnerClass()
item to test:
view.inner_class.some_method.assert_called_once()
The Problem:
I can't properly create a MockView
class to get correct mock_view_instance
during execution.
I've tried 1:
- Returns real object (result of Mock call) without
assert_called_once
method.
mock_instance = Mock(wraps=View, spec=View, spec_set=True)
I've tried 2:
inner_class
not exists (no entering to__init__
method).
mock_instance = Mock(spec=View, spec_set=True)
I've tried 3:
- Ok but
View
isnstance can be instantiated withoutarg
- it's error prone. arg
not exists at this moment, it will be defined dynamically during a test call itself.
mock_instance = Mock(spec=View(arg='foo'), spec_set=True)
I've tried 4:
TypeError: 'View' object is not callable
.
mock_instance = Mock(wraps=View(arg='foo'))
I've tried 5:
- TypeError: 'NonCallableMagicMock' object is not callable
(No matterinstance=True/False
)
mock_instance = create_autospec(spec=View(arg='foo'))
My dirty solution:
# Real object with mock methods (wrapper)
wrapper_instance = Mock(wraps=View, spec_set=True)
# Return mocked object as instance
# (don't use "wrapper_instance" as spec, it will cause infinite recursion).
wrapper_instance.side_effect = Mock(spec=Mock(wraps=View))
P.S. I'm preferring not to use patch
because it's very implicit.
My architecture allows to set any required object during configuration.