ここで、django.template の Context を使用すると、少し混乱する状況になりました。
以下は、Python シェルで機能します。
>>> from django.template import Context, Template
>>> b=Template('TEST').render(Context())
>>> print b
TEST
単体テストでまったく同じコードを使用すると、次のエラーが発生します。
Traceback (most recent call last):
File "/newsletterapi/tests.py", line 25, in setUp
b = Template('TEST').render(Context())
File "/opt/python2.7/lib/python2.7/site-packages/django/template/base.py", line 121, in render
context.render_context.push()
AttributeError: 'Context' object has no attribute 'render_context'
単体テストは次のようになります。
from django.test import TestCase
from myproject.newsletterapi.models import Newsletter
from django.utils.termcolors import colorize
from django.db import IntegrityError
from django.template import Template, Context
import random
import datetime
from decimal import *
import string
class NewsletterTest(TestCase):
def setUp(self):
b = Template('TEST').render(Context()) # this is line 25
self.newsletter = Newsletter(body=b)
self.newsletter.save()
### ... continues here
これがシェルでは機能するのに単体テストでは機能しない理由を誰かが知っていますか? すべてのヒントに感謝します。