How can I reference nested functions in MagicMock.<method>.assert_called_with()
?
I want to mock an object (called here EventObject
) that can distribute events to installed event handlers.
The signature for handler installation looks like this:
class EventObject:
def install_handler(handler_func):
pass
A function that installs as event handler a nested function:
def setup_handler(event_obj):
def handle_event():
pass
event_obj.install_handler(handle_event)
Now how to test this? I tried:
def test_listen():
event_obj = MagicMock(spec=EventObject)
setup_handler(event_obj)
event_obj.install_handler.assert_called_with(setup_handler.handle_event)
But this gives AttributeError: 'function' object has no attribute 'handle_event'
.
Any ideas?