爲什麼我的兩個測試有衝突,我有點不知所措。一次測試中的補丁似乎堅持了另一次測試。如果我對修補測試發表評論,或者只是單獨運行其他測試,則沒有問題。我的測試是:補丁持續跨多個Django單元測試嗎?
class TestUrlRouting(TestCase):
@patch('myapp.views.Home')
def test_home_url_routes_to_home_view(self, mock_home_view):
url = reverse('home')
match = resolve(url)
assert match.func == mock_home_view.as_view()
class TestHomeView(TestCase):
def test_front_page_renders_home_template(self):
response = self.client.get('/')
self.assertTemplateUsed(response, 'home.html')
隨着Home
視圖是一個TemplateView
。我最終的錯誤是:
Traceback (most recent call last):
File "/path/to/code/source/myapp/tests/test_views.py", line 11, in test_front_page_renders_home_template
response = self.client.get('/')
File "/path/to/code/virtualenv/lib/python3.5/site-packages/django/test/client.py", line 503, in get
**extra)
File "/path/to/code/virtualenv/lib/python3.5/site-packages/django/test/client.py", line 304, in get
return self.generic('GET', path, secure=secure, **r)
File "/path/to/code/virtualenv/lib/python3.5/site-packages/django/test/client.py", line 380, in generic
return self.request(**r)
File "/path/to/code/virtualenv/lib/python3.5/site-packages/django/test/client.py", line 467, in request
six.reraise(*exc_info)
File "/path/to/code/virtualenv/lib/python3.5/site-packages/django/utils/six.py", line 686, in reraise
raise value
File "/path/to/code/virtualenv/lib/python3.5/site-packages/django/core/handlers/base.py", line 242, in get_response
response = self.apply_response_fixes(request, response)
File "/path/to/code/virtualenv/lib/python3.5/site-packages/django/core/handlers/base.py", line 305, in apply_response_fixes
response = func(request, response)
File "/path/to/code/virtualenv/lib/python3.5/site-packages/django/http/utils.py", line 17, in conditional_content_removal
if 100 <= response.status_code < 200 or response.status_code in (204, 304):
TypeError: unorderable types: int() <= MagicMock()
因爲我沒有做什麼特別的補丁裝飾,我的猜測是,它有事情做與Django是如何設置的東西了。特別是,我發現補丁myapp.urls.Home
不起作用。相反,我需要補丁myapp.views.Home
,儘管被測線路在urls.py
爲url(r'^$', Home.as_view(), name='home'),
。任何想法,爲什麼這個補丁是持久的,我該如何解決它?非常感謝!
更新: 問題嘲諷myapp.views.Home.as_view
,而不是僅僅myapp.views.Home
時(再次,注意views
是正確嘲笑的選項,而不是urls
出於某種原因)仍然存在,甚至。