IRC'de bu sorunu çözemedim, buradaki bazı yönergeleri bulabileceğimi umuyordum.Birim Sınama Önyükleme Modal Yönlendirmesi
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)
O self.assertContains(response, name_validation_error)
üzerinde başarısız oluyor: Aşağıdaki test var. Form geçersiz olduğunda
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
views.py içinde Bence hataları ile hops.html
şablonu oluşturur: Burada iz geri döndü el site üzerinden tıkladığınızda
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})
, tam olarak ne olsun ben 'm arıyoruz: modaldan ana atlama sayfa listesi listesine, add_hops.errors hatalarının sırasız bir listesini içeren üst kısımdaki Bootstrap uyarı kutusuyla bir yönlendirme. İstek sonrası (self.client.post('url', data={invalid data})
) yanıtı yazdırdım ve yalnızca kalıcı formu içerir. Bu testi bitirmek için uygun yöntem ne olurdu? Veya form doğrulamamı yeniden yazmam gerekiyor mu?
terminalinizi temizleyin, 'response response 'sonra' print response' yazın ve sonra sadece bu testi çalıştırın. Sonra, iddia ettiğiniz şeyin basılı çıktıda olup olmadığını kontrol edin - ilk iddianızda başarısız olmanız nedeniyle, her şey olabilir, örn. sayfa değil vb. –
da url'leri tersine çevirmek için iyi bir fikir '/ beerdb/add/hops ' –
Sorun, şüphelendiğim gibi response.context [' request ']. 'İstek' olarak okunur: