Reputation: 81
I'm using Django 1.11, Python 3.6 and Visual Studio and I'm trying to run my unit tests but I have some problems testing view methods for processing forms.
That's my test:
def test_post_newComment(self):
# Login
self.client.login(username="user1", password="user1")
# Data form
data = {'title': 'Test', 'queja':2}
# Response to post client request
response = self.client.post(reverse('newComment'), data)
# Check response
self.assertTrue(isinstance(response, HttpResponseRedirect))
self.assertEqual(response.status_code, 302)
....
And here's my Views.py method where debugging I can see that request.POST is a empty dict so the form is never filled in the self.client.post.
@login_required(login_url='/login/')
def nuevoComentario(request):
assert isinstance(request, HttpRequest)
# User validation
if not (request.user.is_authenticated):
return HttpResponseRedirect('/login/')
# If the forms has been submitted
if (request.method == 'POST'):
form = NewCommentForm(request.POST)
if (form.is_valid()):
...
The request.POST is always an empty dict. I have tried to add "content_type" parameter to the self.client.post request but I also obtain the same result (request.POST reachs empty to the views.py method).
Upvotes: 3
Views: 948
Reputation: 1
It looks like what you are missing is that data=data
in your client.post
Upvotes: -3