lundi 6 juillet 2015

Passing self-filled data explicitly in POST request

I want to test redirection of Update View.

Reading the documentations, and looking at source code, I find: Update View instances self.object before processing the request (which helps it to set form fields to current value)

Now this is behavior I wanted, but is giving me trouble during testing.

My Models.py

class Project(models.Model):

    title = models.CharField(max_length=50)
    description = models.CharField(max_length=200, blank=True, default="")
    # Other fields

views.py

class UpdateProject(LogInRequiredMixin, UpdateView):

    form_class = ProjectUpdateForm
    template_name = 'project/create.html'

And In Testing: (I have created a project and set up most things in setUp)

def test_redirection(self):
        # After updating project, users should land on correct page.[View Project page].
        self.client.login(username=self.owner.email, password=self.password)
        response = self.client.post(self.url, follow=True) # This will give error, since it requires Title field
        self.assertRedirects(response, reverse('project:show',
                                                kwargs={'project_id': self.project.id}))

Now, I know I can pass title explicitly with data field, but if I were to change the fields later in model, this test would also fail, which it shouldn't [It's only purpose is to check for redirection, there are other tests which deal with form validation, etc]

So, my question is: Is there a way to simulate Post Request as done by Update View (i.e. pass data context by setting object context to pre-filled values and only over-ride changed values]

Aucun commentaire:

Enregistrer un commentaire