Commits

Branko Vukelic committed 24f4ed5

Use mock when testing rendering of email message

Comments (0)

Files changed (1)

tests/test_email.py

 
 
 class TemplateMessage(TestCase):
-    def test_message_from_template_works(self):
+    @patch('contact.email.to_plain_text')
+    @patch('contact.email.render_to_string')
+    def test_message_from_template_works(self, render, to_plain):
         text, html = email.create_message_body('email_test.html', {
             'title': 'Title',
             'message': 'This is a message',
             'sender': 'Postman'
         })
-        self.assertEqual(text, email.to_plain_text(html))
-
-    def test_message_from_template_text_only(self):
-        text1, html1 = email.create_message_body('email_test.html', {
-            'title': 'Title',
-            'message': 'This is a message',
-            'sender': 'Postman'
-        })
-        text2, html2 = email.create_message_body('email_test.html', {
+        self.assertTrue(render.called)
+        self.assertTrue(to_plain.called)
+        self.assertEqual(html, render.return_value)
+        self.assertEqual(text, to_plain.return_value)
+
+    @patch('contact.email.to_plain_text')
+    @patch('contact.email.render_to_string')
+    def test_message_from_template_text_only(self, render, to_plain):
+        text, html = email.create_message_body('email_test.html', {
             'title': 'Title',
             'message': 'This is a message',
             'sender': 'Postman'
         }, text_only=True)
-        self.assertEqual(html2, None)
-        self.assertEqual(html1, text2)
+        self.assertTrue(render.called)
+        self.assertFalse(to_plain.called)
+        self.assertEqual(text, render.return_value)
+        self.assertEqual(html, None)
 
 
 class Message(TestCase):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.