5π
I was stung by this issue just now, ended up finding the solution in the documentation
class-based views need to be compared by name, as the functions generated by as_view() wonβt be equal due to different object ids, so the assertion should look like the below:
from django.test import TestCase
from django.urls import resolve
from .views import HomePageView
class HomePageViewViewTest(TestCase):
def test_resolve_to_home_page_view(self):
resolver = resolve('/')
self.assertEqual(resolver.func.__name__, HomePageView.as_view().__name__)
π€Kingsley Ijomah
3π
from django.urls import resolve, reverse
class HomePageViewViewTest(TestCase):
def test_resolve_to_home_page_view(self):
resolver = resolve('/')
self.assertEqual(resolver.func.view_class, HomePageView)
You can try this, it worked for me!
π€Hetvee Sanghani
- [Django]-Cumulative (running) sum of field Django and MySQL
- [Django]-Django-allauth returns error "Reverse β¦ with arguments '()' and keyword arguments '{}' not found"
- [Django]-How to add class attribute to ModelForm
- [Django]-Unable to use curl to get a token with Django OAuth Toolkit
- [Django]-App specific default settings in Django?
0π
Since you are testing a Class based View, from the Traceback it can be seen that itβs missing the request object. You can use the RequestFactory provided by the django.test package. Better read the following RequestFactory Documentation to get a good view of it. It will solve your problem.
π€AR7
- [Django]-Django Authenticate Backend Multiple Databases
- [Django]-Difficulty installing Django Debug Toolbar
- [Django]-Extract values from Django <QuerySet> in Python 3
- [Django]-Querying for timestamp field in django
- [Django]-Django date YYYY-MM-DD
-1π
from django.urls import resolve, reverse
class HomePageTest(TestCase):
def test_root_url_resolves_to_home_page_view(self):
response = self.client.get(resolve('/'))
response = self.client.get(reverse('your_app_name:list'))
self.assertEqual(response.status_code, 200)
π€user13964893
Source:stackexchange.com