V
V
Vladislav2020-06-08 13:51:55
Flask
Vladislav, 2020-06-08 13:51:55

Why does 'Response' object has no attribute 'items' error in tests?

The test itself

@pytest.fixture
def client():
    app.config['TESTING'] = True
    with app.test_client() as cl:
        with app.app_context():
            yield cl

def test_auth(client):
    payload = {
        'username': 'testing',
        'password': 'hard_pass'
    }
    resp = client.post('/login', data=jsonify(payload))
    assert resp.status_code == 200

views.py currently looks like this
@app.route('/login', methods=['POST'])
def login():
    return 'This is your login'

The error is the following:
d = <Response 46 bytes [200 OK]>, args = (), kwargs = {}

>   iteritems = lambda d, *args, **kwargs: iter(d.items(*args, **kwargs))
E   AttributeError: 'Response' object has no attribute 'items'

blog_env\lib\site-packages\werkzeug\_compat.py:135: AttributeError

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladislav, 2020-06-08
@nitron_5

The problem itself was with
data=jsonify(payload)
jsonify(payload) formed . It was necessary to immediately pay attention to this, because the code did not go further than client.post. Removed jsonify and the test was successful.
<Response 46 bytes [200 OK]>

I
iddqda, 2020-06-08
@iddqda

because the functions decorated with @app.route should return a Response object
in short like this is correct

return Response("This is your login", status=200, content_type="text/plain")

or if json then it's easier like this:
return jsonify({"message": "This is your login"}), 200

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question