mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-11-24 17:15:05 +08:00
11b6e1ba98
Co-authored-by: Ju4tCode <42488585+yanyongyu@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: StarHeart <starheart233@gmail.com>
3.0 KiB
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
定义事件响应器预期立即结束当前事件处理依赖并等待接收一个新的事件后再次执行当前事件处理依赖。
适用事件响应器操作:reject
、reject_arg
和 reject_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()