Şu anda yarattığım sahte bir web sitesi için Django testleri oluşturmaya çalışıyorum.Django test test kullanıcısını kabul etmiyor
class ListingsTestCase(TestCase):
def test_listing(self):
user = User.objects.create_user(username='test')
user.set_password('test')
user.save()
c = Client()
c.login(username='test', password='test')
category = Category.objects.get_or_create(name='test')
t_listing = {
'title': 'Test',
'email': '[email protected]',
'phone_number': '4057081902',
'description': 'Test',
'category': category,
'user': user,
}
form = ListingForm(data=t_listing)
self.assertEqual(form.errors, {})
self.assertEqual(form.is_valid(),True)
Benim modelim: Burada
class Listing(models.Model):
user = models.ForeignKey(User)
title = models.CharField(max_length = 200)
email = models.EmailField(max_length=50)
phone_number = models.CharField(max_length=12, default='')
listing_price = models.IntegerField(blank=True, null=True)
image = models.FileField(upload_to='listing_images')
description = models.TextField()
created_date = models.DateTimeField(auto_now=True)
category = models.ForeignKey('Category', null=True)
def __str__(self):
return self.title
benim ListingForm geçerli:
FAIL: test_listing (seller.tests.ListingsTestCase)
Traceback (most recent call last):
File "/home/local/CE/mwilcoxen/project/hermes/seller/tests.py", line 35, in test_listing
self.assertEqual(form.errors, {})
AssertionError: {'category': [u'Select a valid choice. That choice is not one of the available choices.'], 'user': [u'Select a valid choice. That choice is not one of the available choices.']} != {}
: Burada
class ListingForm(forms.ModelForm):
image = forms.FileField(required=False)
class Meta:
model = Listing
fields = [
'user',
'title',
'email',
'phone_number',
'description',
'listing_price',
'image',
'category',
]
widgets = {'user': forms.HiddenInput()}
Ve alıyorum hatadır
İşte benim sınavım
Bu yüzden, ilk hataların ne olduğunu görmek için ilk assertEquals'ı kullandım ve test kullanıcımın giriş yapabildiğini bilmek için bazı sınırlamalar kullandım, ancak bir nedenden ötürü işe yaramadı. Bana birileri yardım ederse, bu harika olurdu.
sizin 'ListingForm' kodu söyler misin? –
ListingForm'u yapıştırabilir misiniz? –
az önce yayınladı. –