AttributeError:“字节”对象没有属性“状态”

时间:2019-05-29 08:53:47

标签: python

我在测试文件夹中的所有测试中都遵循ex52 LPTHW和疲惫的鼻子测试,出现以下错误:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/nose/case.py", line 198, in runTest
    self.test(*self.arg)
  File "/Users/bijing/projects/gothonweb/tests/app_tests.py", line 17, in test_game
    assert_response(resp.data)
  File "/Users/bijing/projects/gothonweb/tests/tools.py", line 5, in assert_response
    assert status in resp.status,"Excepted response %r not in %r" %(status,resp.status)
AttributeError: 'bytes' object has no attribute 'status'

app.py


urls=(
    '/game','GameEngine',
    '/','Index',
)

app=web.application(urls,globals())

#title hack so that debug mode works with sessions
if web.config.get('_session')is None:
    store=web.session.DiskStore('sessions')
    session=web.session.Session(app,store,initializer={'room':None})
    web.config._session=session
else:
    session=web.config_session

render=web.template.render('templates/',base="layout")

class Index(object):
    def GET(self):
        #this is used to "setup" the session with starting values
        session.room=map.START 
        web.seeother("/game")

class GameEngine(object):
    def GET(self):
        if session.room:
            return render.show_room(room=session.room)
        else:
            #Why is there here?do you need it?
            return render.you_died()
    def POST(self):
        form=web.input(action=None)
        #there is a bug here,can you fix it?
        if session.room and form.action:
            session.room=session.room.go(form.action)
        else:
            session.room=None


if __name__=="__main__":
    app.run()

tools.py

import re

def assert_response(resp,contains=None,matches=None,headers=None,status="200"):
    assert status in resp.status,"Excepted response %r not in %r" %(status,resp.status)
    if status=="200":
        assert resp.data,"Response data is empty."
    if contains:
        assert contains in resp.data,"Response dose not contain %r"%contains
    if matches:
        reg=re.compile(matches)
        assert reg.matches(resp.data),"Response does not match %r"%matches
    if headers:
        assert_equal(resp.headers,headers)

app_test.py

from bin.app import app
from tests.tools import assert_response

def test_index():
    #check that we get a 303 on the /URL
    #this is because web.seeother()will always send the browser this http code
    resp=app.request("/")
    assert_response(resp,status="303")
def test_game():
    #checkt that we get a 200 on /game
    resp=app.request("/game")
    assert_response(resp,status='200')

    #check that we have response data on a form submit
    resp=app.request ("/game",method="POST")
    assert_response(resp.data)

1 个答案:

答案 0 :(得分:0)

assert_response(resp.data)

您要在此处传递bytes数据,其中assert_response需要一个响应对象。像其他所有情况一样,传递resp本身。