mock_slack_webhook

safir.testing.slack.mock_slack_webhook(hook_url, respx_mock)

Set up a mocked Slack server.

Parameters:
  • hook_url (str) – URL for the Slack incoming webhook to mock.

  • respx_mock (Router) – The mock router.

Returns:

The mock Slack webhook API object.

Return type:

MockSlackWebhook

Examples

To set up this mock, put a fixture in conftest.py similar to the following:

import pytest
import respx
from safir.testing.slack import MockSlackWebhook, mock_slack_webhook

@pytest.fixture
def mock_slack(
    config: Config, respx_mock: respx.Router
) -> MockWebhookSlack:
    return mock_slack_webhook(config.slack_webhook, respx_mock)

This uses respx to mock the Slack webhook URL obtained from the configuration of the application under test. Use it in a test as follows:

@pytest.mark.asyncio
def test_something(
    client: AsyncClient, mock_slack: MockSlackWebhook
) -> None:
    # Do something with client that generates Slack messages.
    assert mock_slack.messages == [{...}, {...}]