Hi everybody,
I am trying to test the following model:
class Abono(models.Model): # Abonos en dinero del detallista
detallista = models.ForeignKey(CustomUser, on_delete=models.CASCADE, related_name='abonos')
fecha = models.DateField(default=timezone.now)
monto = models.DecimalField(decimal_places=2, max_digits=6)
capture = models.ImageField(upload_to='images/colecciones/captures_abonos', null=True)
def get_absolute_url(self):
return reverse('abono_detail', kwargs={'pk': self.pk})
This model has a Genric View:
class AbonoCreateView(LoginRequiredMixin, SuccessMessageMixin, CreateView):
"""
Vista que permite el registro de abonos a la empresa por parte del detallista
"""
model = Abono
template_name = 'detallistas/abono_form.html'
fields = ('monto', 'capture', )
exclude = ('detallista', 'fecha')
success_message = '''Abono registrado satisfactoriamente.
Nuestro equipo procederá en breve a la verificación
y posterior aprobación o rechazo del mismo. Gracias'''
def get_success_url(self):
return reverse('abono_list')
def form_valid(self, form):
form.instance.detallista = self.request.user
return super().form_valid(form)
After I made my own research finally I could write this test:
class AbonoCreateViewTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.detallista = get_user_model().objects.create_user(
username = 'saul',
email = 'saul@ejemplo.com',
password = '123456',
)
# cls.now = date.toda
# cls.abono = AbonoFactory(detallista=cls.detallista)
# cls.abono2 = AbonoFactory(detallista=cls.detallista)
def setUp(self):
self.client.login(username='saul', password='123456')
def test_urls_exists_at_correct_location(self):
response = self.client.get('/detallistas/abono/add/')
self.assertEqual(response.status_code, 200)
def test_url_available_by_name (self):
response = self.client.get(reverse('abono_add'))
self.assertEqual(response.status_code, 200)
def test_post_success(self):
temp_file = tempfile.NamedTemporaryFile()
test_image = get_temporary_image(temp_file)
data = {
'monto': 100,
'capture': test_image.name
}
response = self.client.post(reverse('abono_add'), data=data)
self.assertEqual(response.status_code, 302)
But I am getting the following error:
$ coverage run manage.py test detallistas.test.test_views.AbonoCreateViewTest
Creating test database for alias 'default'...
Found 3 test(s).
System check identified no issues (0 silenced).
F..
======================================================================
FAIL: test_post_success (detallistas.test.test_views.AbonoCreateViewTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\goyo\Documents\django\cromoland\detallistas\test\test_views.py", line 233, in test_post_success
self.assertEqual(response.status_code, 302)
AssertionError: 200 != 302
----------------------------------------------------------------------
Ran 3 tests in 2.175s
using a print statement to see the content of the response, I found that it’s throwing a validation message indicating the file that contains the Image can’t be empty.
I do not really how to solve this, I need some help please.