そこで、このコードを Flaskに翻訳することで、Flask の TDD を学ぼうとしています。しばらくの間、テンプレートを文字列にレンダリングする方法を見つけようとしています。これが私が試したことです:
render_template(...)
render_template_string(...)
make_response(render_template(...)).data
そして、それらのどれも機能していないようです。
それぞれの場合のエラーは
"...templating.py", line 126, in render_template
ctx.app.update_template_context(context)
AttributeError: 'NoneType' object has no attribute 'app'
の関数ですtemplating.py
。render_template
私のテストコードは次のとおりです。
def test_home_page_can_save_POST_request(self):
with lists.app.test_client() as c:
c.get('/')
rv = c.post('/', data = {'item_text':"A new list item"})
# This test works
self.assertIn("A new list item", rv.data)
# This test doesn't
self.assertEqual(rv.data,flask.make_response(flask.render_template('home.html',new_item_text='A new list item')).data)
次のhome.html
ように:
<html>
<body>
<h1>Your To-Do list</h1>
<form method="POST">
<input name="item_text" id="id_new_item" placeholder="Enter a to-do item" />
</form>
<table id="id_list_table">
<tr><td>{{ new_item_text }}</td></tr>
</table>
</body>
</html>
編集:エラーは使用されている実際の機能とは無関係である可能性があるため、さらにファイルを追加しました。私はCeleoが彼の答えで提案したものを正確に使用しています。