简体   繁体   中英

Reset ember-data FixtureAdapter store/models in tests

Our app uses ember-data with the FixtureAdapter. We're testing the model code (various JS calculations) using Jasmine and js-test-driver. Each it block creates its own set of records in a beforeEach block.

This works fine with ember-1.0.0-rc.1 and ember-data built locally on 2013-03-05 (rev 7575f5a). I'm currently attempting to upgrade to 1.0.0-rc.3 and the latest ember-data, but am hitting a roadblock with unique ids. I'm getting the following error when running all the tests:

Error: assertion failed: The id 1001 has already been used with another record of type .Foo. in http://localhost:56390/js/lib/ember-1.0.0-rc.3.js (line 52)

It looks like a newer version of ember-data asserts on unique ids. Unfortunately, our tests require hardcoded ids for records that are recreated in the beforeEach block of each test.So each test is creating a Foo with id 1001.

Is there an easy way to make the store with a FixtureAdapter reset all of its data (and consequently forget about Foo 1001)? I tried resetting the fixtures with:

App.Foo.FIXTURES = []

However, ember-data still seems to keep track of the old records somewhere. I also tried App.reset() , but it doesn't seem to reset the store.

We also do

App.Store = DS.Store.extend({
    revision: 12,
    adapter: 'DS.FixtureAdapter'
});

in the beforeEach block with the assumption that that would clear all the data by creating a new store, but that doesn't work either.

I initially ran into the exact same issue, trying to generate records before individual tests. It is helpful to think of fixtures in the Rails sense , where they represent a set of records that will be constructed once before all tests are run.

That is opposed to the factory style of building individual records before each test, which is the pattern you seem to be following.

It is more of a work around than a solution, but I'd recommend placing all of your fixtures into one or more files and then loading those before all tests. You would then reference the same record from separate tests without reconstructing them.

The biggest caveat to the fixture approach is that there isn't any "rolback" provided after each test. As a result you need to be wary of mutating records if you are relying on the original values in your tests.

确保在每次测试运行之间运行Ember.reset()(通过将它放在jasmine beforeAll块之前)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM