Answer the question
In order to leave comments, you need to log in
How to mock a model (queryset) in django?
Hello.
I use the standard library: django.test and unittest.mock, I want to check the following point:
class Dynamical_Pages(TestCase):
def test_advertisement(self):
response = self.client.get('/advertisement')
self.assertEqual(response.status_code, 200)
File "C:\python35\lib\site-packages\django\db\models\query.py", line 387, in get
self.model._meta.object_name
faceset.models.DoesNotExist: Banner matching query does not exist.
class Banner(models.Model):
bancode = models.TextField()
banlink = models.TextField()
banimg = models.ImageField()
from django.test import TestCase
from django.test import Client
import factory
from faceset.models import Banner
#factory_boy data
class BannerFactory(factory.Factory):
class Meta:
model = Banner
bancode = 'test_code_for_deploy'
banlink = 'test_link_for_deploy'
banimg = ''
class Dynamic_pages(TestCase):
def test_upmenu(self):
Banner = BannerFactory.create()
response = self.client.get('/upmenu')
self.assertEqual(response.status_code, 200)
Answer the question
In order to leave comments, you need to log in
It is necessary to create test objects before executing the get request in the test.
def test_advertisement(self):
Banner.objects.create()
response = self.client.get('/advertisement')
self.assertEqual(response.status_code, 200)
Tests require a separate test database.
https://docs.djangoproject.com/en/1.10/topics/test...
https://docs.djangoproject.com/en/1.10/topics/test...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question