Search code examples
pythonpython-3.xpyramid

How to unit test request.json_body in pyramid?


I use the field request.json_body to retrieve the encoded json body like:

@view_config(route_name='reminder', renderer='json', permission='view', xhr=True, request_method='POST')
def reminder(request):
    process(body.request.json_body)
    return {'result':'OK'}

How can I unit test this (I am a python newbie, using 3.4.4). I used DummyRequest() from pyramid testing, but when running the test it complaints:

'DummyRequest' object has no attribute 'json_body'

Which I understand as I read that the DummyRequest is restricted. And how can I fill the "test" request with some json body? I think I am looking in the wrong corners as I can't (google) good info about this :(


Solution

  • You can give it as constructor params while creating the object.

    testing.DummyRequest(json_body=json_params, method='POST')
    

    This should work. Works fine for me