我正在处理的 django 网络项目陷入了死胡同,我似乎找不到任何答案。我正在尝试测试一个像这样简单的视图:
def list(request):
return JsonResponse( {"foo": "bar"} )
Run Code Online (Sandbox Code Playgroud)
似乎一切顺利。如果我在浏览器上打开网站并检查页面信息,它会显示“类型:应用程序/json”。
但是,当我在 travis ci 上运行以下测试时:
def setUpTestData(cls):
cls.client = Client()
#A few lines of setting up test-data
def test_content_type(self):
response = self.client.get('/api/list')
self.assertEqual(response['content-type'], 'application/json')
Run Code Online (Sandbox Code Playgroud)
我得到以下失败:
FAIL: test_content_type (researchlibrary.api.tests.test_list.ListTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/travis/build/FUB-HCC/ACE-Research-Library/researchlibrary/api/tests/test_list.py", line 25, in test_content_type
self.assertEqual(response['content-type'], 'application/json')
AssertionError: 'text/html' != 'application/json'
- text/html
+ application/json
Run Code Online (Sandbox Code Playgroud)
网址都很好。测试收到正确的页面,只是类型似乎是 text/html 而不是 application/json,我不知道为什么会这样。
任何人都知道为什么会这样?
编辑:将 self.client.get('/api/list') 更改为 self.client.get('/api/list/') 解决了问题。