好的,所以我需要对视图进行单元测试,在视图中更精确的形式。所以我创建了这样一个单元测试。
class ViewTest(TestCase):
fixtures = ['fixture.json']
def setUp(self):
self.client = Client()
def test_company_create(self):
post_data = {
'form-0-user': '',
'form-0-share': '',
'form-TOTAL_FORMS': 1,
'form-INITIAL_FORMS': 0,
'form-MAX_NUM_FORMS': 10
}
resp = self.client.post('/company/create/', post_data)
self.assertFormError (resp, 'shareholder_formset', 'share', 'This field is required.')
self.assertFormError (resp, 'shareholder_formset', 'user', 'This field is required.')
当然我得到一个错误
AttributeError:“ShareholderFormFormSet”对象没有属性“字段”
因为formset里面有表单,而不是字段......那么测试formset的正确方法是什么?