I'm using factory boy for the testing my user (Customer). I have created class UserFactoryCustomer for my customer User.
# factories.py
class UserFactoryCustomer(factory.django.DjangoModelFactory):
class Meta:
model = User
django_get_or_create = ('first_name', 'last_name', 'username', 'email', 'user_type', 'balance')
first_name = 'Ahmed'
last_name = 'Asadov'
username = factory.LazyAttribute(lambda o: slugify(o.first_name + '.' + o.last_name))
email = factory.LazyAttribute(lambda a: '{0}.{1}@example.com'.format(a.first_name, a.last_name).lower())
user_type = 1
balance = 10000.00
# test_serializers.py
class ApiSerilizerTestCase(TestCase):
def test_get_status_code_200(self):
customers = factories.UserFactoryExecutor.objects.all()
for customer in customers:
self.assertEqual(customer.get('username').status_code, 200)
I'm getting this mistake
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
..E
======================================================================
ERROR: test_get_status_code_200 (tests.test_serializers.ApiSerilizerTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/heartprogrammer/Desktop/freelance-with-api/freelance/tests/test_serializers.py", line 20, in test_get_status_code_200
customers = factories.UserFactoryExecutor.objects.all()
AttributeError: type object 'UserFactoryExecutor' has no attribute 'objects'
----------------------------------------------------------------------
Ran 3 tests in 0.246s
FAILED (errors=1)
I want to bring all my customers who are in the class and UserFactoryCustomer to test them
When running a TestCase in django, the database used is a temporary database different from the one used in production. This means you can't query for all the User instances in your database.
UserFactoryCustomer does not have a objects
attribute because it's not a django model class. Instead, you can use it to create instances of User model in the test database, and get them using User.objects.all()
To create such instances see the documentation