我有一个继承 CreateView 的类视图
class CreateErrandView(ErrandFormMixin, CreateView):
form_class = ErrandForm
@method_decorator(ensure_csrf_cookie)
def dispatch(self, *args, **kwargs):
print("passes through here")
return super(CreateErrandView, self).dispatch(*args, **kwargs)
def form_valid(self, form, formset):
print("create some transactions")
print("not pass through here")
return super(CreateErrandView, self).form_valid(form, formset)
def post(self, *args, **kwargs):
print("passes through here")
return super(CreateErrandView, self).post(*args, **kwargs)
我需要编写一个发布帖子并执行 form_valid 方法的测试
我写
class ErrandTransactionsTest(TestCase):
def setUp(self):
self.c = Client()
def test__test_valid_form(self):
# Arrange
form_data = {
u'type': 'taxi'
}
form = ErrandForm(data=form_data)
# Act
self.assertEqual(form.is_valid(), True)
response = self.c.post(reverse('errands_create'), form_data)
# Assert
self.assertEqual(len(Transactions.objects.all()), 5)
但它不起作用。
感谢您的任何建议