python属性错误怎么改-凯发k8官方网
我正在做一个遗留的django项目,其中有一个类定义如下from django.http import httpresponse
class response(httpresponse):
def __init__(self, template='', calling_context='' status=none):
self.template = template
self.calling_context = calling_context
httpresponse.__init__(self, get_template(template).render(calling_context), status)
这个类在视图中使用如下def some_view(request):
#do some stuff
return response('some_template.html', requestcontext(request, {'some keys': 'some values'}))
这个类的创建主要是为了在单元测试中使用它来执行断言,也就是说,它们不是使用django.test.client来测试视图,而是创建一个模拟请求,并将其传递给测试中的视图(将视图称为可调用的),如下所示def test_for_some_view(self):
mock_request = create_a_mock_request()
#call the view, as a function
response = some_view(mock_request) #returns an instance of the response class above
self.assertequals('some_template.html', response.template)
self.assertequals({}, response.context)
问题是,在测试套件(相当大的测试套件)的中途,一些测试在执行return response('some_template.html', requestcontext(request, {'some keys': 'some values'}))
堆栈跟踪是self.template = template
attributeerror: can't set attribute
整个堆栈跟踪看起来像======================================================================
error: test_should_list_all_users_for_that_specific_sales_office
----------------------------------------------------------------------
traceback (most recent call last):
file "/users/austiine/projects/mped/console/metrics/tests/unit/views/sales_office_views_test.py", line 106, in test_should_list_all_users_for_that_specific_sales_office
response = show(request, sales_office_id=sales_office.id)
file "/users/austiine/projects/mped/console/metrics/views/sales_office_views.py", line 63, in show
"sales_office_users": sales_office_users}))
file "/users/austiine/projects/mped/console/metrics/utils/response.py", line 9, in __init__
self.template = template
attributeerror: can't set attribute
实际的失败测试是def test_should_list_all_users_for_that_specific_sales_office(self):
user_company = companyfactory.create()
request = self.mock_request(user_company)
#some other stuff
#calling the view
response = show(request, sales_office_id=sales_office.id)
self.assertin(user, response.calling_context["sales_office_users"])
self.assertnotin(user2, response.calling_context["sales_office_users"])
显示视图的代码def show(request, sales_office_id):
user = request.user
sales_office = []
sales_office_users = []
associated_market_names = []
try:
sales_office = salesoffice.objects.get(id=sales_office_id)
sales_office_users = user.objects.filter(userprofile__sales_office=sales_office)
associated_market_names = market.objects.filter(id__in= (sales_office.associated_markets.all())).values_list("name", flat=true)
if user.groups.all()[0].name == userprofile.company_ao:
associated_market_names = [market.name for market in sales_office.get_sales_office_user_specific_markets(user)]
except:
pass
return response("sales_office/show.html", requestcontext(request, {'keys': 'values'}))
总结
以上是凯发k8官方网为你收集整理的的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: vspython版本控制_python
- 下一篇: