這可以使用signals完成。我將在這裏重現代碼片段:
import unittest
from app import app
from flask import template_rendered
from contextlib import contextmanager
@contextmanager
def captured_templates(app):
recorded = []
def record(sender, template, context, **extra):
recorded.append((template, context))
template_rendered.connect(record, app)
try:
yield recorded
finally:
template_rendered.disconnect(record, app)
class TestFoo(unittest.TestCase):
def test_foo(self):
with app.test_client() as c:
with captured_templates(app) as templates:
r = c.get('/foo/')
template, context = templates[0]
self.assertEquals(context['foo'], 'bar')
這裏是另一種實現方式,消除了template
一部分,並把它變成一個迭代器。
import unittest
from app import app
from flask import template_rendered
from contextlib import contextmanager
@contextmanager
def get_context_variables(app):
recorded = []
def record(sender, template, context, **extra):
recorded.append(context)
template_rendered.connect(record, app)
try:
yield iter(recorded)
finally:
template_rendered.disconnect(record, app)
class TestFoo(unittest.TestCase):
def test_foo(self):
with app.test_client() as c:
with get_context_variables(app) as contexts:
r = c.get('/foo/')
context = next(context)
self.assertEquals(context['foo'], 'bar')
r = c.get('/foo/?foo=bar')
context = next(context)
self.assertEquals(context['foo'], 'foo')
# This will raise a StopIteration exception because I haven't rendered
# and new templates
next(context)
這將返回誤報,如果在HTML被渲染詞「欄」其他地方,即使'foo'被設置爲「欄中的」編輯 –
以外的東西。一切都取決於使用。當然,我們可以使用很多技巧來完成它,但如果我們可以簡單地做到這一點,那爲什麼不呢? – turkus
我明白你的觀點。我發佈的答案很難看,總共有12行,並且需要閱讀信號文檔以瞭解其工作原理;而你的是一個班輪。我想我會爭辯說,你的答案是將視圖耦合到後端單元測試;例如,如果將來您將「hello」改爲「hi」,那麼您必須記得在測試中也這樣做。 –