如何在 Ember 中测试路由的 willTransition 动作?

How to test route's willTransition action in Ember?

如何在 Ember 中测试此代码?请大体解释一下这个概念。

// app/routes/products/new.js
import Ember from 'ember';

export default Ember.Route.extend({
  model() {
    return this.store.createRecord('product');
  },
  actions: {
    willTransition() {
      this._super(...arguments);
      this.get('controller.model').rollbackAttributes();
    }
  }
});

我不知道怎么做。可能是路线中的存根模型?我在路由测试中发现商店不可用

在Ruby和RSpec之后,所有这些新的javascript世界有点混乱)但我还是想学习它。

在单元测试中,想法是存根所有外部依赖项。在 ember 你可以这样做:

// tests/unit/products/new/route-test.js
test('it should rollback changes on transition', function(assert) {
  assert.expect(1);
  let route = this.subject({
    controller: Ember.Object.create({
      model: Ember.Object.create({
        rollbackAttributes() {
          assert.ok(true, 'should call rollbackAttributes on a model');
        }
      })
    })
  });
  route.actions.willTransition.call(route);
});

基本上你存根控制器和模型将它们传递给 this.subject(),然后调用你正在测试的任何函数(在这种情况下你必须使用 call 或 apply 来调用具有正确范围的动作),然后断言 rollbackAttributes() 被调用。

assert.expect(1); 在测试开始时告诉 QUnit 正好等待 1 个断言。