I am currently using Ember Data fixtures to drive my test environment. This is very convenient because I don’t have to worry about setting up mock AJAX requests that respond with the correct JSON to every request made during tests. However, AFAIK there is no mechanism for resetting the state of the store, so even though I reset the Ember app after every test, changes to the fixtures (after they have been loaded for the first time) persist. This is obviously not ideal, since tests are no longer sandboxed – mutating/deleting fixtures can affect later tests in the suite.
- Am I misunderstanding the purpose of Ember Data fixtures? Are they not intended to be used for testing?
- Is there a way to reset the store that I’ve been unable to find?
This fiddle illustrates my issue (you may need press run again after the page loads since the tests are run asynchronously): Ember Latest - JSFiddle - Code Playground
Solution: just reset the fixture data between tests.
I had incorrectly assumed that the fixture data was copied into memory and lived in the store somewhere (and therefore changing the fixture data later would have no effect). In reality the fixture data is directly manipulated by the the fixture adapter, so simply re-initializing them to their original state – by reassigning the FIXTURES
arrays – achieves the desired result. Derp.
I’ve updated the fiddle to demonstrate (look for the injectFixtures
function): Ember Latest - JSFiddle - Code Playground
1 Like
I’m running into this same issue, would you still recommend this solution, or has something in Ember changed since?
I’ve stopped using the fixture adapter completely since I posted this question. I work with a highly non-standard and unstable API and therefore have a number of custom adapters and serializers which weren’t being tested at all (and whose logic I ended up having to patch into the fixture adapter – bad idea).
Instead, I would recommend either taking the response mocking approach described in this thread, or if your API is fairly standard, using EAK’s api-stub (currently a bit half-baked and in the process of being replaced with an API Fixture Proxy. Either way, be prepared to write a lot of custom code to achieve what you want; testing Ember apps is like the wild west right now, especially when it comes to the data layer in acceptance tests.
Also, regardless of whether you decide to continue using the fixture adapter or not, I would recommend avoiding calling App.reset()
between tests and just creating a new app instance and tearing it down with App.create()
/App.destroy()
. It will introduce more overhead – and slightly longer test times – but in my experience it’s better than battling with lingering app state issues. Again, see EAK’s testing setup.
Awesome, good ideas, thanks for the reply.