lundi 28 mars 2016

Unit Testing Bootstrap Modal Redirect

I've been unable resolve this issue on IRC, hoping I could find some guidance here. I have the following test:

def test_validation_errors_return_hops_list_page(self):
    response = self.client.post(
        '/beerdb/add/hops',
        data={
            'name': '',
            'min_alpha_acid': '',
            'max_alpha_acid': '',
            'country': '',
            'comments': ''
        }, follow=True
    )

    self.assertEqual(response.status_code, 200)
    self.assertTemplateUsed(response, 'homebrewdatabase/addhops.html')
    name_validation_error = escape("A hop name is required")
    min_alpha_acid_error = escape("You must enter a min alpha acid")
    max_alpha_acid_error = escape("You must enter a max alpha acid")
    country_error = escape("You must enter a country")
    comments_error = escape("You must enter a comment")

    self.assertContains(response, name_validation_error)
    self.assertContains(response, min_alpha_acid_error)
    self.assertContains(response, max_alpha_acid_error)
    self.assertContains(response,country_error)
    self.assertContains(response, comments_error)

It's failing on self.assertContains(response, name_validation_error). Here's the trace back:

Failure
Traceback (most recent call last):
File "/Users/USER/workspace/PycharmProjects/hashtagbrews/homebrewdatabase/tests/test_views.py", line 189, in test_validation_errors_return_hops_list_page
self.assertContains(response, name_validation_error)
File "/Users/USER/workspace/Envs/hashtagbrews/lib/python3.4/site-packages/django/test/testcases.py", line 398, in assertContains
msg_prefix + "Couldn't find %s in response" % text_repr)
AssertionError: False is not true : Couldn't find 'A hop name is required' in response

My view in views.py renders the addhops.html template with errors when the form is invalid:

def addhops(request):

    add_form = HopForm(request.POST or None)

    if request.method == 'POST':
        if add_form.is_valid():
            Hop.objects.create(name=request.POST['name'],
                           min_alpha_acid=request.POST['min_alpha_acid'],
                           max_alpha_acid=request.POST['max_alpha_acid'],
                           country=request.POST['country'],
                           comments=request.POST['comments']
                           )
        return redirect('hops_list')
    else:
        hops_list = Hop.objects.all()
        return render(request, 'homebrewdatabase/hops.html', {'hops': hops_list, 'form': add_form})
return render(request, 'homebrewdatabase/addhops.html', {'form': add_form})

When I manually click through the site, I get exactly what I'm looking for: a redirect from the modal to the main hops page list with a Bootstrap alert box at the top containing an unordered list of errors from add_hops.errors. I've printed out the response after the post request (self.client.post('url', data={invalid data})) and it only contains the modal form. What would be the proper method to finish this test? Or do I need to rewrite my form validation?

Aucun commentaire:

Enregistrer un commentaire