Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to erase contents in store() after each test? #83

Closed
njoshi023 opened this issue Aug 11, 2014 · 4 comments
Closed

How to erase contents in store() after each test? #83

njoshi023 opened this issue Aug 11, 2014 · 4 comments

Comments

@njoshi023
Copy link

Hi guys,

I am having an issue emptying out the store across my tests. This causes errors when I define models with the same id across multiple tests. Here is a really simply JSbin to illustrate - http://jsbin.com/qiwev/4/edit

  /*=================== APP ====================*/
Ember.MODEL_FACTORY_INJECTIONS = false;
App = Ember.Application.create();

App.Foo = DS.Model.extend({
});



/*=================== TESTS ====================*/

emq.globalize();
App.setupForTesting();
App.injectTestHelpers();
setResolver(Ember.DefaultResolver.create({ namespace: App }));
App.rootElement = '#ember-testing';


moduleForModel('foo', 'Foo model', {
  teardown: function() {
    // this.store().unloadAll('foo');
  }
});

test('Test 1', function() {
  var store = this.store();

  var model = this.subject({
    id: 1
  });

  ok(true);

});

test('Test 2', function() {
  var store = this.store();

  var model = this.subject({
    id: 1
  });

  ok(true);
});

test('Test 3', function() {
  var store = this.store();

  var model = this.subject({
    id: 1
  });

  ok(true);
});

The error I get is:

Error: Assertion Failed: The id 1 has already been used with another record of type App.Foo.

The commented out this.store().unloadAll('foo'); on teardown doesn't help either. So any suggestions on how I can reset the store across each test? Should that possibly be the default case as well to keep all tests independent? Thanks

@tmcgilchrist
Copy link

We've also had issues with what I call the lifecycle of tests in ember and run across similar issues to this.
Seems to me that there are 2 solutions, neither of which are completely satisfactory:

  1. use more randomised data. Maybe use faker.js or https://github.com/danielspaniel/ember-data-factory-guy
  2. Move each test into their own moduleForModel e.g.
App.Foo = DS.Model.extend({
});


moduleForModel('foo', 'Foo model 1', {
  teardown: function() {
  }
});

test('Test 1', function() {
  var store = this.store();

  var model = this.subject({
    id: 1
  });

  ok(model);

});

moduleForModel('foo', 'Foo model 2', {
  teardown: function() {
  }
});

test('Test 2', function() {
  var store = this.store();

  var model = this.subject({
    id: 1
  });

  ok(model);
});

@fsmanuel
Copy link
Contributor

I made a helper function:

// tests/helpers/store.js

import Ember from 'ember';

const {
  run
} = Ember;

function waitForStore(assert, timeout=50) {
  const done = assert.async();

  run.later(this, function() {
    done();
  }, timeout);
}

export {
  waitForStore
};

my tests look like that:

import Ember from 'ember';
import { test, moduleForModel } from 'ember-qunit';
import { waitForStore } from '../../helpers/store';

const {
  run
} = Ember;

moduleForModel('project', 'Project', {
  needs: ['model:task']
});

test('it creates a simple project', function(assert) {
  expect(1);

  const store = this.store();

  run(this, function() {
    let project = store.createRecord('project', {
      name: 'Simple project'
    });

    assert.equal(project.get('name'), 'Simple project');
    assert.equal(project.get('tasks.length'), 0);
  });

  // extend the timeout waitForStore(assert, 100);
  waitForStore(assert);
});

@gitjeff05
Copy link

@fsmanuel Could you expand a little on what waitForStore actually does? It appears as if you're using waitForStore() to call the async callback after a specified period. And this should give anything in teardown enough time to run? I'm still having trouble getting properties of my object to properly clear out after running Ember.run App, App.destroy

@Turbo87
Copy link
Member

Turbo87 commented Oct 14, 2017

this seems no longer relevant, closing due to age

@Turbo87 Turbo87 closed this as completed Oct 14, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants