summaryrefslogtreecommitdiff
path: root/docs/topics/testing
diff options
context:
space:
mode:
authorAdam Johnson <me@adamj.eu>2020-05-20 11:04:36 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-07-13 11:56:46 +0200
commite906ff6fca291fc0bfa0d52f05817ee9dae0335d (patch)
tree23f2d6788f6fb7b95b9bb3809f4c48138910373f /docs/topics/testing
parentca6c5e5fc23f2855a7094d195f09975b21a7ec3f (diff)
Fixed #30457 -- Added TestCase.captureOnCommitCallbacks().
Diffstat (limited to 'docs/topics/testing')
-rw-r--r--docs/topics/testing/tools.txt36
1 files changed, 36 insertions, 0 deletions
diff --git a/docs/topics/testing/tools.txt b/docs/topics/testing/tools.txt
index a22428962a..741acd604c 100644
--- a/docs/topics/testing/tools.txt
+++ b/docs/topics/testing/tools.txt
@@ -881,6 +881,42 @@ It also provides an additional method:
previous versions of Django these objects were reused and changes made
to them were persisted between test methods.
+.. classmethod:: TestCase.captureOnCommitCallbacks(using=DEFAULT_DB_ALIAS, execute=False)
+
+ .. versionadded:: 3.2
+
+ Returns a context manager that captures :func:`transaction.on_commit()
+ <django.db.transaction.on_commit>` callbacks for the given database
+ connection. It returns a list that contains, on exit of the context, the
+ captured callback functions. From this list you can make assertions on the
+ callbacks or call them to invoke their side effects, emulating a commit.
+
+ ``using`` is the alias of the database connection to capture callbacks for.
+
+ If ``execute`` is ``True``, all the callbacks will be called as the context
+ manager exits, if no exception occurred. This emulates a commit after the
+ wrapped block of code.
+
+ For example::
+
+ from django.core import mail
+ from django.test import TestCase
+
+
+ class ContactTests(TestCase):
+ def test_post(self):
+ with self.captureOnCommitCallbacks(execute=True) as callbacks:
+ response = self.client.post(
+ '/contact/',
+ {'message': 'I like your site'},
+ )
+
+ self.assertEqual(response.status_code, 200)
+ self.assertEqual(len(callbacks), 1)
+ self.assertEqual(len(mail.outbox), 1)
+ self.assertEqual(mail.outbox[0].subject, 'Contact Form')
+ self.assertEqual(mail.outbox[0].body, 'I like your site')
+
.. _live-test-server:
``LiveServerTestCase``