繁体   English   中英

在Ember.RSVP.hash中使用过滤器无法在qunit测试中检索数据

[英]Using filter inside Ember.RSVP.hash fails to retrieve data in qunit test

我在使用两个模型并使用Ember.RSVP.hash的路由上进行了qunit测试。 事情按预期工作,但是一旦我向Ember.RSVP.hash添加filter ,它就会停止检索qunit中的数据。 在qunit之外,它仍然可以工作,因此我认为这是filterEmber.RSVP.hashqunit

这是我的测试:

module('Ember.js Library', {
  setup: function() {
    Ember.run(App, App.advanceReadiness);
  },
  teardown: function() {
    App.reset();
  }
});

test('foo', function() {
  visit('/books');
  andThen(function() {
    equal(currentRouteName(), 'books.index');
  });
});

这是不使用filter的工作路线:

App.BooksRoute = Ember.Route.extend({
  model: function() {
    var id = 1;

    // note this does not filter "books" properly
    return Ember.RSVP.hash({
      books: this.store.find('books'),
      library: this.store.find('library', id)
    });
  }
});

这是使用filter的版本,无法使用:

App.BooksRoute = Ember.Route.extend({
  model: function() {
    var id = 1;

    // note: filters "books" properly but doesn't work in qunit
    return Ember.RSVP.hash({
      books: this.store.filter('books', function(record) {
        return record.get('libraryId') === id;
      }),
      library: this.store.find('library', id)
    });
  }
});

最终结果是,在工作版本中, books按预期方式存在。 在第二版中, books最终是空的。 在这两个版本中, library始终存在,因此我相信filter是问题所在。

我如何使用filter并使qunit正常工作?

store.filter实际上不会加载记录,它只会返回商店中已经存在的记录。 您可能想做的是...

App.BooksRoute = Ember.Route.extend({
  model: function() {
    var id = 1;

    // note: filters "books" properly but doesn't work in qunit
    return Ember.RSVP.hash({
      books: this.store.find('books').then(function(books){
        return books.filterBy('libraryId', id)
      }),
      library: this.store.find('library', id)
    });
  }
});

(免责声明:上面的代码未经测试,但这就是想法)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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