1 回答

TA貢獻1851條經驗 獲得超4個贊
Flask discord 服務器上的一位樂于助人的人能夠為我回答這個問題。
問題是 Flask-wtforms 不傳遞模型的整個實例,而是只傳遞主鍵。解決方案是只傳遞數據字典中的主鍵,如下所示:
class TestingWhileLoggedIn(TestCase):
def create_app(self):
app = c_app(TestConfiguration)
return app
# executed prior to each test
def setUp(self):
self.app_context = self.app.app_context()
self.app_context.push()
db.create_all()
login(self.client, '******', '*****')
# executed after each test
def tearDown(self):
db.session.remove()
db.drop_all()
self.app_context.pop()
logout(self.client)
def test_add_post_page_li(self):
p_cat = PostCategory(name='Resources')
p_cat1 = PostCategory(name='Ressdgources')
p_cat2 = PostCategory(name='Ressdgsdgources')
p_cat3 = PostCategory(name='Reurces')
db.session.add(p_cat)
db.session.add(p_cat1)
db.session.add(p_cat2)
db.session.add(p_cat3)
db.session.commit()
all_cats = PostCategory.query.all()
self.assertEqual([p_cat,p_cat1,p_cat2,p_cat3], all_cats)
response = self.client.get('/add_post', follow_redirects=False)
self.assertEqual(response.status_code, 200)
# the following line was changed from having category=p_cat to
# category=p_cat.id
data = dict(title='Hello', content='fagkjkjas', category=p_cat.id)
#
# The following code has been commented out since it is no longer needed
#
# form = PostForm(data=data)
#
# this would not pass anymore
# self.assertEqual(form.validate(), True)
#
# printing the data to see what it is
# print(form.data)
# This line was changed from having data=form.data to data=data
response_1 = self.client.post('/add_post', follow_redirects=False, data=data, content_type='multipart/form-data')
# this one fails
self.assertEqual(response_1.status_code, 302)
new_post = db.session.query(Post).filter_by(name='Hello').first()
self.assertNotEqual(new_post, None)
添加回答
舉報