nonebot / nonebot/nonebug

BUG: `ctx.create_bot` 未能按预期创建 Bot 对象

Open
#57 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
30
Forks
7
Avg merge
2d 17h
Merged PRs (30d)
1

Description

使用文档中测试平台接口使用的例子

from datetime import datetime

import nonebot
import pytest
from nonebot import on_command
from nonebot.adapters.console import Bot, User, Adapter, Message, MessageEvent
from nonebug import App
from pytest_asyncio import is_async_test


def pytest_collection_modifyitems(items: list[pytest.Item]):
    pytest_asyncio_tests = (item for item in items if is_async_test(item))
    session_scope_marker = pytest.mark.asyncio(loop_scope='session')
    for async_test in pytest_asyncio_tests:
        async_test.add_marker(session_scope_marker, append=False)


@pytest.fixture(scope='session', autouse=True)
async def after_nonebot_init(after_nonebot_init: None) -> None:
    from nonebot.adapters.console import Adapter as ConsoleAdapter

    # 加载适配器
    driver = nonebot.get_driver()
    driver.register_adapter(ConsoleAdapter)


def make_event(message: str = "") -> MessageEvent:
    return MessageEvent(
        time=datetime.now(),
        self_id="test",
        message=Message(message),
        user=User(id="user"),
    )


@pytest.mark.asyncio
async def test_example(app: App):
    foo = on_command("foo")

    @foo.handle()
    async def _(bot: Bot):
        await foo.send("message")
        await bot.bell()

    async with app.test_matcher(foo) as ctx:
        adapter = nonebot.get_adapter(Adapter)
        bot = ctx.create_bot(base=Bot, adapter=adapter)
        event = make_event("/foo")
        ctx.receive_event(bot, event)
        ctx.should_call_send(event, "message", result=None, bot=bot)
        ctx.should_call_api("bell", {}, result=None, adapter=adapter)

异常信息如下:

Launching pytest with arguments var/test_foo.py::test_example --no-header --no-summary -q in C:\Users\Administrator\Project\...

============================= test session starts =============================
collecting ... collected 1 item


======================== 1 failed, 1 warning in 2.74s =========================

var/test_foo.py::test_example FAILED                                     [100%]
var\test_foo.py:46 (test_example)
app = <nonebug.app.App object at 0x000001C524EC7A10>

    @pytest.mark.asyncio
    async def test_example(app: App):
        foo = on_command("foo")
    
        @foo.handle()
        async def _(bot: Bot):
            await foo.send("message")
            await bot.bell()
    
        async with app.test_matcher(foo) as ctx:
            adapter = nonebot.get_adapter(Adapter)
>           bot = ctx.create_bot(base=Bot, adapter=adapter)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

var\test_foo.py:57: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
.venv\Lib\site-packages\nonebug\mixin\call_api\__init__.py:100: in create_bot
    bot = make_fake_bot(self, base=base)(adapter, self_id, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <[RecursionError('maximum recursion depth exceeded') raised in repr()] FakeBot object at 0x1c5250e19a0>
adapter = Adapter(name='Console'), info = 'test'

    def __init__(self, adapter: "Adapter", info: Robot):
>       super().__init__(adapter, info.id)
                                  ^^^^^^^
E       AttributeError: 'str' object has no attribute 'id'

.venv\Lib\site-packages\nonebot\adapters\console\bot.py:66: AttributeError

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with ctx.create_bot in nonebug/mixin/call_api/init.py and compare it with the console adapter's Bot.init shown in nonebot/adapters/console/bot.py. Reproduce the documented test example and trace the arguments passed into the fake bot. Done means the example can create a Bot without the reported AttributeError and continue to its assertions.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.