AttributeError: can't set attribute

后端 未结 3 612
夕颜
夕颜 2021-01-03 21:55

I am working on a legacy django project, in there somewhere there is a class defined as follows;

from django.http import HttpResponse

class Response(HttpRes         


        
3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-03 22:10

    I took a look to django source code I've no idea where template or templates attribute come from in HttpResponse. But I can propose to you to change your test approach and migrate to mock framework. You can rewrite your test like:

    @patch("qualified_path_of_response_module.response.Response", spec=Response)
    def test_should_list_all_users_for_that_specific_sales_office(self,mock_resp):
        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.assertTrue(mock_resp.called)
        context = mock_resp.call_args[0][2]
        self.assertIn(user, context["sales_office_users"])
        self.assertNotIn(user2, context["sales_office_users"])
    

    @patch decorator replace your Response() class by a MagicMock() and pass it to your test method as mock_resp variable. You can also use patch as context manager by with construct but decorators are the cleaner way to do it. I don't know if Response is just a stub class for testing but in that case you can patch directly HttpResponce, but it depends from your code.

    You can find details about call_args here. Maybe you need to use spec attribute because django make some type checking... but try with and without it (I'm not a django expert). Explore mock framework: it'll give to you lot of powerful tools to make simple tests.

提交回复
热议问题