3👍
Resolve will return the function that is returned when calling HomePage.as_view()
, and not an object of that type. However, from a quick test there may be a way that you could write this test:
self.assertEquals(found.func.func_name, HomePage.__name__)
Note that here we specify HomePage.__name__
instead of 'HomePage'
because this will get picked up if the name of the class is changed using refactoring tools.
The downside of this is, should you have wired up a view class with the same name but from a different module, this unit test would not fail. Of course this is more of a risk with a generic view class name such as HomePage
but should be less of a risk with other view classes.
21👍
Django’s View.as_view()
creates a function with a view_class
attribute which points to the class-based view. So use:
self.assertEquals(found.func.view_class, HomePage)
Avoids the problem of two class-based views in different modules with the same name.
- Hide password field in GET but not POST in Django REST Framework where depth=1 in serializer
- Newline in label for Django form field
4👍
May be it’s an old question, but in django>=1.8 assertion like
self.assertEquals(found.func.func_name, HomePage.__name__)
AttributeError: ‘function’ object has no attribute ‘func_name’ so I changed it to
self.assertEqual(found.func.__name__, HomePage.__name__)
- Django disable editing (but allow adding) in TabularInline view
- Django – How can you include annotated results in a serialized QuerySet?
- Customize logging for external/third-party libs
- DRF test client unable to post list of JSON
0👍
I did it another way because Valentjedi did not work for me;
I did .:
class HomePageTest(TestCase):
def test_root_url_resolves_to_home_page_view(self):
found = resolve('/')
self.assertEqual(found.view_name, "home")
Hope it helps