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

This is a good paper: http://www.jmock.org/oopsla2004.pdf - in particular sections 4.1 and 5.2 are relevant to this discussion.

@blaix
Copy link

blaix commented Oct 13, 2015

@blaix
Copy link

blaix commented Oct 13, 2015

This is a good paper: http://www.jmock.org/oopsla2004.pdf - in particular sections 4.1 and 5.2 are relevant to this discussion.

The paper mentions wrappers for types you don't own, but at some point you have to stop wrapping (or you'd go infinitely deep). That's the boundary. It should be thin. And you want to keep it thin because it should be integration tested.

I say "because it should be integration tested" which sounds like I'm making concessions to my design purely for easier testing, but I use that phrase as short-hand for something that is better designed and therefore easier to test. The better design of thin boundaries being decoupling, particularly from components you don't have control over.

@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