python属性错误怎么改_属性错误:无法设置属性

我正在做一个遗留的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'}))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值