Skip to content

Instantly share code, notes, and snippets.

@beck
Last active October 13, 2015 10:21
Show Gist options
  • Save beck/5c417fd1672a497540d1 to your computer and use it in GitHub Desktop.
Save beck/5c417fd1672a497540d1 to your computer and use it in GitHub Desktop.
import unittest
from mock import patch, Mock
from django.shortcuts import render
class Dashboard(object):
"""This is the system under test."""
@classmethod
def view(cls, request):
return render(request, 'dashboard.html')
class DashboardView_WhenTestedUsingStubs(unittest.TestCase):
@patch('__main__.render')
def setUp(self, render):
render.return_value = Mock(content='<html>dashboard')
self.response = Dashboard.view(request=Mock())
def test_returns_a_response_with_a_rendered_dashboard(self):
self.assertEqual(self.response.content, '<html>dashboard')
class DashboardView_WhenTestedUsingMocks(unittest.TestCase):
@patch('__main__.render')
def setUp(self, render):
self.request = Mock()
self.render = render
Dashboard.view(self.request)
def test_renders_a_request_with_a_template(self):
self.render.assert_called_with(self.request, 'dashboard.html')
if __name__ == '__main__':
unittest.main()
@blaix
Copy link

blaix commented Oct 13, 2015

I like to have long conversations with myself on github. It is my power.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment