Question

I have simple model class that I store some global state with

  App.Person = Ember.Object.extend({
      firstName: '',
      lastName: ''
  });

  App.Person.reopenClass({
      people: [],
      add: function(hash) {
          var person = App.Person.create(hash);
          this.people.pushObject(person);
      },  
      remove: function(person) {
          this.people.removeObject(person);
      },  
      find: function() {
          var self = this;
          $.getJSON('/api/people', function(response) {
              response.forEach(function(hash) {
                  var person = App.Person.create(hash);
                  Ember.run(self.people, self.people.pushObject, person);
              }); 
          }, this); 
          return this.people;
      }
  });

When I invoke App.reset() using RC6 and ember-testing I notice whatever state is in the global people array stays around between tests. I do see a log that shows teardown is invoked between tests, it just doesn't clear the # of people. How can I reset this in the QUnit teardown?

  module('integration tests', {                                                
      setup: function() {
          Ember.testing = true;
          this.server = sinon.fakeServer.create();
          this.server.autoRespond = true;
          Ember.run(App, App.advanceReadiness);
      },
      teardown: function() {
          this.server.restore();
          App.reset(); //won't kill that global state ...
      }   
  });

Update

If you want to mock the "/" route in RC6 a bug will prevent your model hook from firing again after you mock the xhr (I hope to see this fixed in RC7+)

https://github.com/emberjs/ember.js/issues/2997

Était-ce utile?

La solution

App.reset only destroys Ember's generated objects. Classes are untouched.

You will need to extend the reset method and do this cleanup manually.

App = Ember.Application.create({
  reset: function() {
    this._super();
    App.Person.people = [];
  }
});
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top