Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple Mock Test Example #20

Open
fractal-joe opened this issue Jan 22, 2023 · 0 comments
Open

Simple Mock Test Example #20

fractal-joe opened this issue Jan 22, 2023 · 0 comments

Comments

@fractal-joe
Copy link

Hi,
Thanks for the awesome library! I'm trying to do assert that on_message was called on publish.

from unittest.mock import create_autospec

import pytest
from asyncio_paho import AsyncioPahoClient


async def on_connect_async(client, _1, _2, _3) -> None:
    client.subscribe("mytopic")


async def on_message(_, _1, msg):
    print(f"Received from {msg.topic}: {str(msg.payload)}")


mock_on_message = create_autospec(on_message)


@pytest.mark.asyncio
async def test_pubsub():
    async with AsyncioPahoClient(client_id="paho 🐝") as client:
        client.asyncio_listeners.add_on_connect(on_connect_async)
        await client.asyncio_connect("localhost")
        client.asyncio_listeners.add_on_message(mock_on_message)
        client.publish("mytopic", "foo")
        mock_on_message.assert_called_once()

However, I'm getting AssertionError: Expected 'mock' to have been called once. Called 0 times.

Thanks for taking a look :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant