gpt4 book ai didi

ember.js - 如何在 Ember 中测试路由的 willTransition Action ?

转载 作者:行者123 更新时间:2023-12-04 20:33:51 24 4
gpt4 key购买 nike

如何在 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();
}
}
});

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

在 Ruby 和 RSpec 之后,所有这些新的 JavaScript 世界都有些困惑)但我还是想学习它。

最佳答案

在单元测试中,想法是 stub 所有外部依赖项。在 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);
});

基本上你 stub Controller 和模型将它们传递给 this.subject() ,然后调用您正在测试的任何函数(在这种情况下,您必须使用 call 或 apply 来调用具有正确范围的操作),然后断言 rollbackAttributes()被称为。
assert.expect(1);在测试开始时告诉 QUnit 正好等待 1 个断言。

关于ember.js - 如何在 Ember 中测试路由的 willTransition Action ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39961328/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com