Question: I am trying to test that my search View renders the results from my search correctly, however, the search uses the indexes from my live database and not my test database, so when I create some objects for my test case, they are not reflected on the search page.

How can I make haystack use an index of the search database, or better still, just fake it and not use an index, but instead use the database as is. This would be fine for that test case and probably faster.

I could only seem to find this article when googling: <http://reliablybroken.com/b/2012/12/testing-django-haystack-whoosh/>
and it does not work with the current versions.

```
Pip versions:
django==1.7.5
django-haystack==2.4.0

```

Answer: 
I've encountered a similar use case in our project. Here's the rough idea of our implementation. Note that if you use the `simple_backend` (SB), some of your custom filters/prepare method [may not work as expected](http://django-haystack.readthedocs.io/en/latest/tutorial.html#simple). Hence, it's advisable to use non-SB backend (e.g. `Elastic Search`) even in testing mode.

```
from django.core.urlresolvers import reverse
from django.test import TestCase
from django.test.utils import override_settings

from haystack import connections
from haystack.utils.loading import ConnectionHandler, UnifiedIndex

from myapp.models import MyModel
from myapp.search_indexes import MyModelIndex

TEST_INDEX = {
    'default': {
        'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
        'URL': 'http://127.0.0.1:9200/',
        'INDEX_NAME': 'test_index',
    },
}

@override_settings(HAYSTACK_CONNECTIONS=TEST_INDEX )
class SearchViewTest(TestCase):
    def setUp(self):
        """
        Some ideas taken from here:
        https://github.com/django-haystack/django-haystack/blob/v2.6.0/test_haystack/test_views.py#L40
        """
        connections = ConnectionHandler(TEST_INDEX )
        super(SearchViewTest, self).setUp()

        self.mm = MyModel.objects.create(name='Dummy Title')

        # Stow.
        self.old_unified_index = connections['default']._index
        self.ui = UnifiedIndex()
        self.mmi = MyModelIndex()
        self.ui.build(indexes=[self.mmi])
        connections['default']._index = self.ui

        # Update the 'index'.
        backend = connections['default'].get_backend() 
        backend.clear()
        backend.update(self.mmi, MyModel.objects.all())

    def tearDown(self):
        connections['default']._index = self.old_unified_index
        super(SearchViewTest, self).tearDown()

    def test_search_results(self):
        response = self.client.get('/search?q=dummy')
        self.assertIn(self.mm.name, response)

```
create a test index and make sure that your haystack configs point to the test index when in test mode.
Something like: if 'test' in sys.argv: 

Besides this you can either add another index in the search engine or use another search engine (such as whoosh) for your tests. 

The other option is to actually mock the response that you get from the search engine and pass that to the views / forms you are trying to test.