Skip to content

Simple Mock Test Example #20

Open
Open
@fractal-joe

Description

@fractal-joe

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 :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions