猎鹰,AttributeError:"API"对象没有属性“创建" [英] falcon, AttributeError: 'API' object has no attribute 'create'

查看:118
本文介绍了猎鹰,AttributeError:"API"对象没有属性“创建"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测试猎鹰路线,但测试始终失败,并且看起来我做对了所有事情.

I'm trying test my falcon routes, but tests always failed, and looks like I make all things right.

我的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' 错误?谢谢.

Help please, why I got AttributeError: 'API' object has no attribute 'create' error? Thanks.

推荐答案

您在app.py中缺少假设 create()函数.

您的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中的内容应类似于:

Then in your tests/static.py should look like:

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

这篇关于猎鹰,AttributeError:"API"对象没有属性“创建"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆