For an empty Queryset, I would just select none as Keithhackbarth already said .
However, to simulate a Queryset that will return a list of values, I prefer to use Mock with the model manager spec . As an example (Python 2.7 style - I used the external Mock library ), here is a simple test in which Queryset is filtered and then counted:
from django.test import TestCase from mock import Mock from .models import Example def queryset_func(queryset, filter_value): """ An example function to be tested """ return queryset.filter(stuff=filter_value).count() class TestQuerysetFunc(TestCase): def test_happy(self): """ 'queryset_func' filters provided queryset and counts result """ m_queryset = Mock(spec=Example.objects) m_queryset.filter.return_value = m_queryset m_queryset.count.return_value = 97 result = func_to_test(m_queryset, '__TEST_VALUE__') self.assertEqual(result, 97) m_queryset.filter.assert_called_once_with(stuff='__TEST_VALUE__') m_queryset.count.assert_called_once_with()
However, to answer this question, instead of setting return_value for count , it can be easily configured to be a list model instances returned from all .
Note that the chain is processed by setting a filter to return a simulated set of queries:
m_queryset.filter.return_value = m_queryset
This must be applied to any query set methods used in the function being tested, for example, exclude , etc.
source share