简体   繁体   English

Ember对测试路线钩子和动作感到困惑

[英]Ember confused on testing route hooks and actions

I am just starting looking at testing with Ember and I am bit confused on how to test the following: 我刚刚开始考虑使用Ember测试,我对如何测试以下内容感到困惑:

1 - Route hooks 1 - 路线挂钩

Here's an example what I have, I am not sure if should be done through unit testing or user acceptance testing? 这是我的一个例子,我不确定是否应该通过单元测试或用户验收测试来完成? How do I triggers the hooks, wait the promise etc.? 如何触发挂钩,等待承诺等?

/**
 * 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 - Controller/Route Actions 2 - 控制器/路由操作

Here I mostly just either going to different route or displaying error message, is this something that should be unit tested? 在这里,我大多只是去不同的路线或显示错误信息,这是应该进行单元测试的东西吗? if so how? 如果是这样的话?

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');
        });
    }
}

You can test going to different routes or error messages with acceptance tests . 您可以使用验收测试来测试不同的路径或错误消息。 This would cover your route's actions and all the model hooks. 这将涵盖您的路线的动作和所有模型钩子。

If you wanted to test your route's actions, unit tests are a good idea. 如果你想测试路线的动作,单元测试是个好主意。 You can send an action to your route and see what you get back. 您可以将动作发送到您的路线并查看您的回复。

As in this example : 如下例所示

// Now use the routes send method to test the actual action route.send('displayAlert', expectedTextBar);

It's a good idea to break your code up into smaller chunks that you can test individually. 将代码分解为可以单独测试的较小块是个好主意。 As the documentation says: "separating code into smaller chunks (or "concerns"), allows it to be more readily isolated for testing, which in turn allows you to catch bugs more easily." 正如文档所说:“将代码分成更小的块(或”关注点“),使其更容易被隔离以进行测试,这反过来又可以让您更容易地捕获错误。”

If your route is getting too complex and hard to test, it may be a sign that you need to abstract some code out into a service or something. 如果您的路线过于复杂且难以测试,则可能表明您需要将某些代码抽象为服务或其他内容。 This will make testing easier also. 这也将使测试更容易。

As a side note: I see you're returning an array of objects in your afterModel hook. 作为旁注:我看到你在afterModel钩子中返回一个对象数组。 That will work just fine without the using RSVP. 没有使用RSVP,这将工作得很好。

Hope that helps. 希望有所帮助。

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

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