【发布时间】:2015-01-08 23:29:53
【问题描述】:
我为金字塔网络框架写了一个login_required 装饰器。在金字塔测试服务器中它运行良好。
但在 @view_config 装饰器的 Pyramid 单元测试中,不适用于所有配置(不仅是装饰器参数)。
这是代码:
class MyViews(object):
@view_config(decorator=login_required(login_url=LOGIN_URL),
match_param="action=change_password", request_method="GET",
renderer="accounts/change_password.jinja2")
def change_password(self):
form = ChangePwdForm()
return {'form': form}
这里是测试代码:
def test_change_password_fail(self):
from .views import AccountsViews
aviews = AccountsViews(testing.DummyRequest(path='/accounts/forget_password'))
response = aviews.forget_password()
self.assertEqual(response.status_code, 307) #HTTPTemporaryRedirect
我的例外是not-logined-user 将被重定向到登录网址。
@view_config 中的所有参数,例如 renderer 和 'match_param' 都不起作用。
我该如何解决这个问题?
参考:
Mocking render to response with Pyramid
Unit and Integration Testing:Pyramid 官方指南,但不涉及基于类的视图的装饰器问题
【问题讨论】:
标签: python unit-testing web pyramid web-frameworks