2

グローバルな状態を保存する単純なモデルクラスがあります

  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;
      }
  });

RC6 と ember-testing を使用して App.reset() を呼び出すと、グローバル people 配列の状態がテスト間で維持されることに気付きます。テスト間でティアダウンが呼び出されたことを示すログが表示されますが、人数がクリアされていません。QUnit のティアダウンでこれをリセットするにはどうすればよいですか?

  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 ...
      }   
  });

アップデート

RC6 で "/" ルートをモックしたい場合、xhr をモックした後にモデル フックが再び発火するのをバグが妨げます (これは RC7+ で修正されることを願っています)。

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

4

1 に答える 1

2

App.resetEmber が生成したオブジェクトのみを破棄します。クラスはそのままです。

リセット メソッドを拡張し、このクリーンアップを手動で行う必要があります。

App = Ember.Application.create({
  reset: function() {
    this._super();
    App.Person.people = [];
  }
});
于 2013-07-11T04:33:05.110 に答える