falcon,AttributeError:“API”对象没有属性“create”

2024-05-17

我正在尝试测试我的猎鹰路线,但测试总是失败,而且看起来我把所有事情都做对了。

my app.py

import falcon
from resources.static import StaticResource


api = falcon.API()
api.add_route('/', StaticResource())

和我的测试目录tests/static.py

from falcon import testing
import pytest
from app import api


@pytest.fixture(scope='module')
def client():
    # Assume the hypothetical `myapp` package has a
    # function called `create()` to initialize and
    # return a `falcon.API` instance.
    return testing.TestClient(api.create())


def test_get_message(client):
    result = client.simulate_get('/')
    assert result.status_code == 200

请帮忙,为什么我得到了AttributeError: 'API' object has no attribute 'create' 错误?谢谢。


你错过了假想 create()功能在你的app.py.

Your app.py应如下所示:

import falcon
from resources.static import StaticResource

def create():
    api = falcon.API()
    api.add_route('/', StaticResource()) 
    return api

api = create()

然后在你的tests/static.py应该看起来像:

from falcon import testing
import pytest
from app import create


@pytest.fixture(scope='module')
def client():
    return testing.TestClient(create())

def test_get_message(client):
    result = client.simulate_get('/')
    assert result.status_code == 200
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

falcon,AttributeError:“API”对象没有属性“create” 的相关文章