【发布时间】:2026-01-17 17:25:01
【问题描述】:
我正在处理一个遗留的 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 来测试视图,而是创建了一个模拟请求并将其传递给 view as(在测试中调用视图作为可调用)如下
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'}))
【问题讨论】:
-
你能告诉我们整个回溯错误信息吗?
-
堆栈跟踪的最大部分只是一堆代码爆炸的测试文件的绝对路径,视图和异常起源的实际文件,但我会在这里粘贴
-
尝试在 self.template = template 之前打印 type(template) 并告诉我打印信息。
-
print type(template) 返回
并且 print type(self.template) 返回 *** AttributeError: 'Response' object has no attribute 'templates' -
在我看来,
self的响应中有一个名为template的只读属性。你能发布sales_office_views.py模块的show()方法吗?
标签: python django unit-testing httpresponse