如果我有这样的表格:
class MyForm(modelForm):
# Form stuff
和类似这样的表单集:
MyFormSet = modelformset_factory(
MyModel,
form=MyForm,
max_num=6,
validate_max=True,
)
是否可以测试form=MyForm
?
def test_formset(self):
formset = MyFormSet()
self.assertEqual(formset.max_num, 6)
self.assertTrue(formset.validate_max)
# Tried this but it didn't work
self.assertIsInstance(formset.form, MyForm)
答案 0 :(得分:1)
在这种情况下,formset.form
将是MyForm
类的子类,而不是它的实例,因此assertIsInstance
将不起作用。您可以使用以下方法简单地检查它:
def test_formset(self):
formset = MyFormSet()
self.assertTrue(issubclass(formset.form, MyForm))
用于formset_factory
,并且当您不想允许提供MyForm
的任何子类时,这也将起作用(因为formset_factory
不会修改表单类):
def test_formset(self):
formset = MyFormSet()
self.assertEquals(formset.form, MyForm)