django 属性错误:"HttpResponse"对象没有属性"render"

w8f9ii69  于 2023-01-10  发布在  Go
关注(0)|答案(1)|浏览(175)

有人知道为什么会发生这个错误吗?我一直在为我的其他测试运行同一行,只有这一行返回一个错误test_coverageReturnSuccess + test_coverageReturnCorrectCoverage中的两行都返回这个错误:

response.render()
AttributeError: 'HttpResponse' object has no attribute 'render'

这是我的API测试用例

class CoverageTest(APITestCase):
    protein = None
    domain = None
    protein_domain = None
    good_url = ''
    bad_url = ''

    def setUp(self):
        self.protein = ProteinFactory.create(pk=1, protein_id='A0A014PQC0', length=338)
        self.domain = PfamFactory.create()
        self.protein_domain = ProteinDomainLinkFactory.create(protein=self.protein, 
                                                            pfam_id=self.domain, 
                                                            start=157, 
                                                            stop=314)
        # Set urls
        self.good_url = reverse('coverage', kwargs={'protein_id': 'A0A014PQC0'})

    def tearDown(self):
        # Reset test tables
        Protein.objects.all().delete()
        Pfam.objects.all().delete()
        ProteinDomainLink.objects.all().delete()
        # Reset primary keys
        ProteinFactory.reset_sequence(0)
        PfamFactory.reset_sequence(0)
        ProteinDomainLinkFactory.reset_sequence(0)

    def test_coverageReturnSuccess(self):
        """
        Ensure we get an 200 OK status code when making a valid GET request.
        """
        response = self.client.get(self.good_url, format='json')
        response.render()
        self.assertEqual(response.status_code, 200)

    def test_coverageReturnCorrectCoverage(self):
        """
        Ensure we get the right coverage of the requested protein
        """
        response = self.client.get(self.good_url, format='json')
        response.render()
        data = json.loads(response.content)
        self.assertEqual(data, 0.46449704142011833)
aiqt4smr

aiqt4smr1#

您不应该 render 响应,因为response已经呈现了,所以例如:

def test_coverageReturnSuccess(self):
    """
    Ensure we get an 200 OK status code when making a valid GET request.
    """
    response = self.client.get(self.good_url, format='json')
    # not response.render()!
    self.assertEqual(response.status_code, 200)

对于JSON,您用途:

def test_coverageReturnCorrectCoverage(self):
    """
    Ensure we get the right coverage of the requested protein
    """
    response = self.client.get(self.good_url, format='json')
    # not response.render()!
    data = response.json()
    self.assertEqual(data, 0.46449704142011833)

相关问题