Skip to content

Instantly share code, notes, and snippets.

@agusmakmun
Forked from dnmellen/test_views.py
Created May 8, 2020 06:57
Show Gist options
  • Save agusmakmun/c4acf33565f92dd942fd25a3ace5acdf to your computer and use it in GitHub Desktop.
Save agusmakmun/c4acf33565f92dd942fd25a3ace5acdf to your computer and use it in GitHub Desktop.

Revisions

  1. @dnmellen dnmellen revised this gist Sep 11, 2013. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions test_views.py
    Original file line number Diff line number Diff line change
    @@ -13,6 +13,8 @@ class DummyView(YourMixin, TemplateView):
    '''
    To test get_context_data we need a TemplateView child
    '''

    template_name = 'any_template.html' # TemplateView requires this attribute

    def setUp(self):

  2. @dnmellen dnmellen created this gist Sep 10, 2013.
    34 changes: 34 additions & 0 deletions test_views.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    from django.test import TestCase, RequestFactory
    from django.views.generic import TemplateView

    from ..lib.views import YourMixin


    class YourMixinTest(TestCase):
    '''
    Tests context-data in a Django Mixin like a boss
    '''

    class DummyView(YourMixin, TemplateView):
    '''
    To test get_context_data we need a TemplateView child
    '''

    def setUp(self):

    super(YourMixinTest, self).setUp()
    self.request = RequestFactory().get('/fake-path')

    # Setup request and view.
    self.view = self.DummyView()

    def test_context_data_no_args(self):

    # Prepare initial params
    kwargs = {}

    # Launch Mixin's get_context_data
    context = self.view.get_context_data(**kwargs)

    # Your checkings here
    self.assertEqual(context['name'], 'foo')