nonebot2/website/versioned_docs/version-2.0.0-beta.5/advanced/unittest/test-matcher-operation.md
github-actions[bot] e5e69c2726 🔖 Release 2.0.0-beta.5
2022-08-04 06:27:40 +00:00

3.0 KiB

sidebar_position description
3 测试事件响应处理

测试事件响应处理行为

除了 send,事件响应器还有其他的操作,我们也需要对它们进行测试,下面我们将定义如下事件响应器操作的预期行为对对应的事件响应器操作进行测试。

should_finished

定义事件响应器预期结束当前事件的整个处理流程。

适用事件响应器操作:finish

示例插件
from nonebot import on_message

foo = on_message()

@foo.handle()
async def _():
    await foo.finish("test")
import pytest
from nonebug import App

@pytest.mark.asyncio
async def test_matcher(app: App, load_plugins):
    from awesome_bot.plugins.example import foo

    async with app.test_matcher(foo) as ctx:
        bot = ctx.create_bot()
        event = make_fake_event()()  # 此处替换为平台事件
        ctx.receive_event(bot, event)
        ctx.should_call_send(event, "test", True)
        ctx.should_finished()

should_paused

定义事件响应器预期立即结束当前事件处理依赖并等待接收一个新的事件后进入下一个事件处理依赖。

适用事件响应器操作:pause

示例插件
from nonebot import on_message

foo = on_message()

@foo.handle()
async def _():
    await foo.pause("test")
import pytest
from nonebug import App

@pytest.mark.asyncio
async def test_matcher(app: App, load_plugins):
    from awesome_bot.plugins.example import foo

    async with app.test_matcher(foo) as ctx:
        bot = ctx.create_bot()
        event = make_fake_event()()  # 此处替换为平台事件
        ctx.receive_event(bot, event)
        ctx.should_call_send(event, "test", True)
        ctx.should_paused()

should_rejected

定义事件响应器预期立即结束当前事件处理依赖并等待接收一个新的事件后再次执行当前事件处理依赖。

适用事件响应器操作:rejectreject_argreject_receive

示例插件
from nonebot import on_message

foo = on_message()

@foo.got("key")
async def _():
    await foo.reject("test")
import pytest
from nonebug import App

@pytest.mark.asyncio
async def test_matcher(app: App, load_plugins):
    from awesome_bot.plugins.example import foo

    async with app.test_matcher(foo) as ctx:
        bot = ctx.create_bot()
        event = make_fake_event()()  # 此处替换为平台事件
        ctx.receive_event(bot, event)
        ctx.should_call_send(event, "test", True)
        ctx.should_rejected()