Reputation: 61
please help to write the test.
to address:
/edit_records/38/
uses the view edit_records().
Here's a test for it:
def test_edit_records(self):
resolver = resolve('/edit_records/38/')
self.assertEqual(resolver.func, edit_records)
And for an address:
/edit_records/38/
POST={
q:1,
w:2
}
view should be used my_records()
here's a non-working test for it:
def test_edit_records_POST(self):
resolver = resolve('/edit_records/38/', {q:1, w:2})
self.assertEqual(resolver.func, my_records)
here's an view, if you want to:
def edit_records(request, id_record):
entry = Diary.get_entry(id_record=id_record, user_id=request.user.pk)
form = addMessageForm(instance=entry)
if request.method == 'POST':
form = addMessageForm(request.POST, request.FILES, instance=entry)
if form.is_valid():
form.save()
return HttpResponseRedirect('/my_records/')
t = loader.get_template('page_edit_records.html')
c = RequestContext(request, {
'form': form,
})
return HttpResponse(t.render(c))
Upvotes: 0
Views: 44
Reputation: 599778
I'm not quite sure why you're doing all that stuff with resolver
. That's not really relevant to a unit test; you don't ever call the view itself, so you're not testing any of its actual functionality, the only thing you're testing here is Django's URL resolver.
Instead, you should use the functionality provided by the test client to actually call your views:
def test_edit_records(self):
response = self.client.get('/edit_records/38/')
self.assertContains(response, 'some data you expect to see in the response')
And you can do the same with the POST:
def test_edit_records_POST(self):
response = self.client.POST('/edit_records/38/', {q:1, w:2})
self.assertTrue(something_about_the_response)
Upvotes: 1