mercredi 24 octobre 2018

How to test Django CreateView and form_valid

This maybe very basic but I still fail to understand how to test a Django CreateView which has a form_valid(self, form) method ?

Here's my code:

class NewPatientFormView(LoginRequiredMixin, CreateView):
    model = Patient
    fields = ['name', 'surname', 'phone', 'email', 'PESEL', 'age',
          'patient_agreement']

    def form_valid(self, form):
        self.object = form.save(commit=False)
        self.object.created_by_user = self.request.user
        self.object.save()
        return super().form_valid(form)`

I'm using pytest and I simply fail to understand how to test this one. I would be really grateful for a short example how this could be tested ... Thanks!

Aucun commentaire:

Enregistrer un commentaire