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

时间:2016-10-10 15:06:28

标签: ember.js qunit ember-testing

如何在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世界都让人感到困惑)但是我还是想学习它。

1 个答案:

答案 0 :(得分:2)

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

// 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个断言。

相关问题