- Django 3.2 中的新函數。
返回一個上下文管理器,它為給定的數據庫連接捕獲
transaction.on_commit()
using
是要為其捕獲回調的數據庫連接的別名。如果
execute
是True
,如果沒有發生異常,所有回調將在上下文管理器退出時被調用。這模擬了包裝代碼塊之後的提交。例如:
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')
在 Django 4.0 中更改:在舊版本中,未捕獲在執行
transaction.on_commit()
本文介紹 django.test.TestCase.captureOnCommitCallbacks
的用法。
聲明
classmethod TestCase.captureOnCommitCallbacks(using=DEFAULT_DB_ALIAS, execute=False)
相關用法
- Python Django TestCase.setUpTestData用法及代碼示例
- Python Tensorflow asin()用法及代碼示例
- Python TextBlob.correct()用法及代碼示例
- Python Tensorflow math.accumulate_n()用法及代碼示例
- Python Tensorflow cosh()用法及代碼示例
- Python TextCalendar prmonth()用法及代碼示例
- Python Tensorflow sin()用法及代碼示例
- Python Tensorflow acos()用法及代碼示例
- Python Tensorflow asinh()用法及代碼示例
- Python Tensorflow nn.softplus()用法及代碼示例
- Python Tensorflow exp()用法及代碼示例
- Python Tensorflow logical_and()用法及代碼示例
- Python Tensorflow logical_or()用法及代碼示例
- Python TextCalendar formatyear()用法及代碼示例
- Python Tensorflow atanh()用法及代碼示例
- Python TextBlob.Word.spellcheck()用法及代碼示例
- Python Tensorflow bitwise.bitwise_and()用法及代碼示例
- Python TextCalendar pryear()用法及代碼示例
- Python TextBlob.noun_phrases()用法及代碼示例
- Python Tensorflow nn.sigmoid()用法及代碼示例
- Python Tensorflow bitwise.invert()用法及代碼示例
- Python TextBlob.word_counts()用法及代碼示例
- Python Tensorflow abs()用法及代碼示例
- Python Tensorflow nn.tanh()用法及代碼示例
- Python Tensorflow acosh()用法及代碼示例
注:本文由純淨天空篩選整理自djangoproject.com大神的英文原創作品 django.test.TestCase.captureOnCommitCallbacks。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。