明示的は暗黙的よりも優れています
これは本質的に同じことを行います。
pre_count = Article.objects.count()
# Your Logic
post_count = Article.objects.count()
self.assertEqual(post_count-pre_count, 1)
または、その特別なルビーのフレーバーのために、
from django.utils.decorators import method_decorator
from contextlib import contextmanager
def ExtendedTestCase(TestCase):
@method_decorator(context_manager)
def assertDifference(self, func, diff, message=None):
" A Context Manager that performs an assert. "
old_value = func()
yield # `with` statement runs here. Roughly equivalent to ruby's blocks
new_value = func()
self.assertEqual(new_value-old_value, diff, message)
def ArticleTestCase(ExtendedTestCase):
def test_article_creation(self):
" Test that new articles can be created "
with self.assertDifference(Article.count, 1):
self.client.post("/article/new/", {
"title": "My First Django Article",
"content": "Boring Technical Content",
})