Using fixtures with ember data during integration testing

I’m looking for some pointers on how to replace my models with fixtures during integration testing.

I have an ember-cli app that is using Ember Data with the Emberfire adapter for Firebase. When my integration tests run, I want to be able to stub out the store to use fixture data instead of hitting the real backend. I understand that the suggested approach is to mock out the http calls, but I obviously don’t want to have to rely upon the internals of how the Firebase adapter communicates with the server. So I have been trying to find a way to somehow replace the store while these tests are running with one that uses a fixtures adapter.

The main problem seems to be that by the time my test module’s setup is running, the singleton store has already been instantiated and hooked up to the firebase adapter, and I can’t work out how to replace either the store or the application adapter in the dependency container.

Am I even going about this the right way, or can you suggest a better approach for me to look at?

2 Likes

I don’t have a lot of ember experience but in the past 2 weeks we have done it 2 different ways:

  1. Switch adapters based on environment
# app/adapters/application.coffee

`import DS from 'ember-data'`
`import config from '../config/environment';`

if config.environment == "test"
  ApplicationAdapter = DS.FixtureAdapter.extend()
else
  ApplicationAdapter = DS.ActiveModelAdapter.extend()

  ApplicationAdapter.reopen
    namespace: 'api/v1'
    host: 'http://localhost:3000'


`export default ApplicationAdapter`

  1. Use Pretender to stub http requests and return your fixture data.

We are using ember-cli-pagination which got us pointed in this direction, they include a helper to convert fixture to a ‘response’. No switching is needed for the adapter, take a look at the following links:

1 Like

Thanks for your suggestions. I have played around with the same idea of testing the environment in the application adapter and using a Fixture Adapter during testing. The main issue with this though is that I would like to use the default Firebase adapter in a few end-to-end tests, but switch to the Fixture adapter for the majority of the feature integration tests. But it looks like only one DataStore is created which is used for all the tests in any run, and so you cannot switch between stores on a test-by-test basis. I guess I could try to work out how to create 2 different testing environments for different sets of tests - perhaps I might look into this possibility.

As for using Pretender to stub the http requests, I would need to delve into the internal behavior of how the Firebase library communicates with the backend and I don’t want to depend on this in my tests.

Thanks again for sharing your experiences anyway.