I'm currently trying to create Django tests for a mock website that I've created.
Here is my test:
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': 'test@test.com',
'phone_number': '4057081902',
'description': 'Test',
'category': category,
'user': user,
}
form = ListingForm(data=t_listing)
self.assertEqual(form.errors, {})
self.assertEqual(form.is_valid(),True)
My model:
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
Here is my ListingForm:
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()}
And here is the error that I am getting:
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.']} != {}
So I used my first assertEquals to be able to see what is throwing errors, and I used some breakpoints to know that my test user is able to login, but for some reason its just not working. If anyone could give me some help that would be great.