Last active
December 30, 2016 11:08
-
-
Save vortec/54a52acaea08e77023d3d670d7d753ee to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EventDispatcher: | |
def __init__(self, loop, input_handler, output_handler): | |
self.loop = loop | |
self.input_handler = input_handler | |
self.output_handler = output_handler | |
async def __call__(self, event): | |
# Generic stuff (logging, benchmarking, ...) | |
message = await self.input_handler(event) | |
await self.output_handler(message) | |
# Current and only way to test if the input handler gets called: | |
@pytest.mark.asyncio | |
async def test_calls_input_handler(loop): | |
async def input_handler(event): | |
input_handler.called = True | |
async def output_handler(_): | |
pass | |
dispatcher = EventDispatcher(input_handler, output_handler) | |
await dispatcher('abc') | |
assert input_handler.called is True | |
# What I would prefer: | |
@pytest.mark.asyncio | |
async def test_calls_input_handler(loop): | |
from unittest.mock import AsyncMock | |
input_handler = AsyncMock() | |
dispatcher = EventDispatcher(input_handler, AsyncMock()) | |
await dispatcher('abc') | |
assert input_handler.called is True | |
# Or even: | |
@pytest.mark.asyncio | |
async def test_calls_input_handler(loop): | |
async def input_handler(event): | |
input_handler.called = True | |
output_handler = async lambda: None | |
dispatcher = EventDispatcher(input_handler, output_handler) | |
await dispatcher('abc') | |
assert input_handler.called is True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment