如何在金字塔中对request.json_body进行单元测试?

edb*_*ras 2 python python-3.x pyramid

我使用字段request.json_body来检索编码的json体,如:

@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'}
Run Code Online (Sandbox Code Playgroud)

我怎样才能对此进行单元测试(我是一个python新手,使用3.4.4).我使用了来自金字塔测试的DummyRequest(),但在运行测试时,它投诉:

'DummyRequest' object has no attribute 'json_body'
Run Code Online (Sandbox Code Playgroud)

当我读到DummyRequest受限制时,我理解这一点.如何用一些json体填充"测试"请求?我想我正在寻找错误的角落,因为我不能(谷歌)关于这个的好信息:(

小智 5

您可以在创建对象时将其作为构造函数params.

testing.DummyRequest(json_body=json_params, method='POST')
Run Code Online (Sandbox Code Playgroud)

这应该工作.对我来说很好