-
-
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() |
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.
I like to have long conversations with myself on github. It is my power.
And a relevant talk: http://confreaks.tv/videos/rubyconf2011-why-you-don-t-get-mock-objects