test if class based view exists unittest

Solutions on MaxInterview for test if class based view exists unittest by the best coders in the world

showing results for - "test if class based view exists unittest"
Frieda
03 Feb 2018
1from django.contrib.auth.models import AnonymousUser, User
2from django.test import RequestFactory, TestCase
3
4from .views import MyView, my_view
5
6class SimpleTest(TestCase):
7    def setUp(self):
8        # Every test needs access to the request factory.
9        self.factory = RequestFactory()
10        self.user = User.objects.create_user(
11            username='jacob', email='jacob@…', password='top_secret')
12
13    def test_details(self):
14        # Create an instance of a GET request.
15        request = self.factory.get('/customer/details')
16
17        # Recall that middleware are not supported. You can simulate a
18        # logged-in user by setting request.user manually.
19        request.user = self.user
20
21        # Or you can simulate an anonymous user by setting request.user to
22        # an AnonymousUser instance.
23        request.user = AnonymousUser()
24
25        # Test my_view() as if it were deployed at /customer/details
26        response = my_view(request)
27        # Use this syntax for class-based views.
28        response = MyView.as_view()(request)
29        self.assertEqual(response.status_code, 200)
30