Reputation: 1604
My TestClass looks like:
from django.test import TestCase, Client
class TestGetSomething(TestCase):
def test_get_first_url(self):
path = "some/long/path"
client = Client()
response = client.get(path=path)
self.assertEqual(response.status_code, 200)
But assertEquals
raises the exception, 404 != 200
.
When i wrote print(response.__dict__)
i have noted request that fields:
'_closable_objects': [<WSGIRequest: GET 'somelong/path'>]
'request': {'PATH_INFO': 'some/long/path', 'REQUEST_METHOD': 'GET', 'SERVER_PORT': '80', 'wsgi.url_scheme': 'http', 'QUERY_STRING': ''}
'templates': [<django.template.base.Template object at 0x7f4b33ac17f0>], 'context': [{'True': True, 'False': False, 'None': None}, {'request_path': '/somelong/path/', 'exception': 'Resolver404'}]
As you can see the part of paths does not have the slash between 'some' and 'long'
When i am trying to get the page by URL manually (using a browser) everything goes OK
In my Django app i do not use nothing except of Model and ModelAdmin. Even urls.py is clear.
Everybody now how can i fixed it?
Any additional code I'll add if it necessary.
Upvotes: 1
Views: 243
Reputation: 476729
Paths should start with a slash /
. In fact in your browser it is no different, since if you write localhost:8000/some/long/path
the first slash is simply part of the path
. Furthermore a browser will typically "fix" a url like example.com
to example.com/
.
You thus need to write it like:
class TestGetSomething(TestCase):
def test_get_first_url(self):
# start with a slash
path = "/some/long/path"
client = Client()
response = client.get(path=path)
self.assertEqual(response.status_code, 200)
Upvotes: 2