Python测试assertContents–AttributeError:“Response”对象没有属性“streaming”

2024-09-30 06:29:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用assertContains测试html响应对象:

from __future__ import unicode_literals

from django.test import TestCase

from .signals import check_mailchimp_campaign
from django.db.models.signals import post_save

from wagchimp.models import WagchimpCampaign, MailchimpSetting

class WagchimpCampaignTestCase(TestCase):

    fixtures = ['sites.json']

    def setUp(self):
        MailchimpSetting.objects.create(api_key='9b69a8e3a3116aea899-us1',
            note='names', site_id='2')
        a = WagchimpCampaign.objects.create(name="jons",
            campaign_id="1", subject_line="New email",
            top_line="Hi")
        apikey = MailchimpSetting.objects.get().api_key
        a.rss_feed.add = 'skeletal'

    def test_mailchimp_api_response_on_signal_handler(self):
        r = check_mailchimp_campaign(post_save)
        status_code = str(r.status_code)
        self.assertEquals(status_code, '200')

它在以下信号中测试与Mailchimp API的连接:

^{pr2}$

我得到了一个错误:

Traceback (most recent call last):
  File "/Users/technical/code/pb/tests.py", line 46, in test_mailchimp_api_response_on_signal_handler
    self.assertContains(r, "", status_code=200)
  File "/Users/technical/.virtualenvs/wagtest6-PGdhJpMT/lib/python2.7/site-packages/django/test/testcases.py", line 385, in assertContains
    response, text, status_code, msg_prefix, html)
  File "/Users/technical/.virtualenvs/wagtest6-PGdhJpMT/lib/python2.7/site-packages/django/test/testcases.py", line 360, in _assert_contains
    if response.streaming:
AttributeError: 'Response' object has no attribute 'streaming'

response对象带有一个200状态代码,并且看起来格式良好。如果没有流属性,为什么会失败?在


Tags: djangofromtestimportselfapiobjectsresponse
1条回答
网友
1楼 · 发布于 2024-09-30 06:29:57

文档中提到的assertContains使用响应实例具体指的是从Django测试客户机返回的内部响应类,如further up that page所述。在

在任何情况下,正如我提到的,您绝对不应该在测试中查询外部api。根据定义,单元测试应该只使用代码单元—其他的都是外部的。测试API是否工作有什么用?您应该测试的是,给定来自API的特定响应,您自己的代码会执行预期的操作。你可以使用mocks。在

相关问题 更多 >

    热门问题