Ember.js Ember在测试路线挂钩和动作时感到困惑
我刚刚开始考虑使用Ember进行测试,对于如何测试以下内容,我有点困惑: 1-路线挂钩 这是我的一个例子,我不确定是应该通过单元测试还是用户验收测试来完成?如何触发钩子,等待承诺等Ember.js Ember在测试路线挂钩和动作时感到困惑,ember.js,ember-qunit,Ember.js,Ember Qunit,我刚刚开始考虑使用Ember进行测试,对于如何测试以下内容,我有点困惑: 1-路线挂钩 这是我的一个例子,我不确定是应该通过单元测试还是用户验收测试来完成?如何触发钩子,等待承诺等 /** * Model hook. */ model() { return this.modelFor('new'); }, /** * AfterModel hook. */ afterModel() { /** * Setup provinces. */ re
/**
* Model hook.
*/
model() {
return this.modelFor('new');
},
/**
* AfterModel hook.
*/
afterModel() {
/**
* Setup provinces.
*/
return new Ember.RSVP.Promise((resolve) => {
const provinces = Ember.A([
Ember.Object.create({
code: 'AB',
description: 'Alberta'
}),
Ember.Object.create({
code: 'BC',
description: 'British Columbia'
}),
Ember.Object.create({
code: 'MB',
description: 'Manitoba'
}),
Ember.Object.create({
code: 'NB',
description: 'New Brunswick'
}),
Ember.Object.create({
code: 'NL',
description: 'Newfoundland and Labrador'
}),
Ember.Object.create({
code: 'NS',
description: 'Nova Scotia'
}),
Ember.Object.create({
code: 'NT',
description: 'Northwest Territories'
}),
Ember.Object.create({
code: 'NU',
description: 'Nunavut'
}),
Ember.Object.create({
code: 'ON',
description: 'Ontario'
}),
Ember.Object.create({
code: 'PE',
description: 'Prince Edward Island'
}),
Ember.Object.create({
code: 'QC',
description: 'Quebec'
}),
Ember.Object.create({
code: 'SK',
description: 'Saskatchewan'
}),
Ember.Object.create({
code: 'YK',
description: 'Yukon'
})
]);
resolve(provinces);
}).then((provinces) => {
this.set('provinces', provinces);
});
},
/**
* Setup controller hook.
* @param controller the controller
* @param model The model
*/
setupController(controller, model) {
this._super(controller, model);
controller.set('provinces', this.get('provinces'));
}
2-管制员/路线行动
这里我主要是去不同的路径或者显示错误消息,这是应该进行单元测试的吗?如果是,怎么做
actions: {
/**
* Go previous step
*/
back() {
this.transitionToRoute('new.step1');
},
/**
* Go to next step.
*/
next() {
this.get('model').save().then(() => {
this.transitionToRoute('new.step3');
}).catch(() => {
this.get('notificationService')
.notifyError('common.error.system_error');
});
}
}
您可以使用测试转到不同的路由或错误消息。这将涵盖路线的动作和所有模型挂钩 如果您想测试路由的操作,单元测试是一个好主意。你可以发送一个动作到你的路线,看看你能得到什么 例如:
//现在使用routes send方法测试实际操作
route.send('displayAlert',expectedTextBar)代码>
最好将代码分成更小的块,以便单独测试。正如文档中所说:“将代码分成更小的块(或“关注点”),可以更容易地将其分离出来进行测试,从而使您更容易捕获bug。”
如果您的路由变得过于复杂且难以测试,这可能表明您需要将一些代码抽象到服务或其他内容中。这也将使测试更容易
作为旁注:我看到您在afterModel
hook中返回了一个对象数组。如果不使用RSVP,这将很好地工作
希望有帮助