Reputation: 21
I'm trying to prepare some simple tests for my app. I have a model as below:
class Kategoria(models.Model):
nazwa = models.CharField('Nazwa Kategorii', max_length=30)
class Meta:
verbose_name = "Kategoria"
verbose_name_plural = "Kategorie"
def __str__(self):
return self.nazwa
In the tests.py:
import unittest
from blog.models import Kategoria
class KategoriaTestCase(unittest.TestCase):
def setUp(self):
self.nazwa = 'Nowa_kategoria'
def test_tworzenie_obiektu(self):
tworzenie_nowej_kategoria=Kategoria.objects.create(self.nazwa)
self.assertTrue(tworzenie_nowej_kategoria)
self.assertEqual(nowa_kategoria.nazwa,'Nowa_kategoria')
On the end test fails because:
TypeError: create() takes 1 positional argument but 2 were given
What am I doing wrong?
Upvotes: 2
Views: 85
Reputation: 61273
create
takes keywords arguments.
In your test_tworzenie_obiektu
method change
tworzenie_nowej_kategoria = Kategoria.objects.create(self.nazwa)
^^^^
to
tworzenie_nowej_kategoria = Kategoria.objects.create(nazwa=self.nazwa)
So you method should be
def test_tworzenie_obiektu(self):
tworzenie_nowej_kategoria = Kategoria.objects.create(nazwa=self.nazwa)
self.assertTrue(tworzenie_nowej_kategoria)
self.assertEqual(nowa_kategoria.nazwa,'Nowa_kategoria')
Upvotes: 2
Reputation: 174708
You need to supply the field name with create()
, like this:
def test_tworzenie_obiektu(self):
tworzenie_nowej_kategoria = Kategoria.objects.create(nazwa=self.nazwa)
self.assertTrue(tworzenie_nowej_kategoria)
self.assertEqual(nowa_kategoria.nazwa,'Nowa_kategoria')
You should also confirm the last self.assertEqual
, it will always fail since there is no nowa_kategoria
variable in your class; you probably want
self.assertEqual(tworzenie_nowej_kategoria.nazwa, self.nazwa)
Note that I removed the hardcoded name, and changed the name of the variable to that of the object being returned.
Upvotes: 1