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)
添加回答
举报