简体   繁体   中英

How to unit test Routes in ember-cli app using qunit?

I can't seem to get the model hooks and actions triggered from a unit test.

Any sample/blog doing this ember-cli environment would be a great help!

I found this link What kind of unit test solution for the routes in Ember.js?

but route.model() is throwing errors as :transition isn't defined.

import { test, moduleFor } from 'ember-qunit';

moduleFor('route:sample', 'SampleRoute', {
  // Specify the other units that are required for this test.
});

test("beforeModel hook works", function(){
    var route = this.subject();
    Ember.run(function(){
        route.set("model", "Sample data");
    })
    console.log("Model set. Was beforeModel hook called?");
});

The Sample Route

import Ember from 'ember';

export default Ember.Route.extend({

    beforeModel: function (transition) {
        console.log("Inside before-model hook");
    },

    afterModel: function() {
        console.log("In after-model hook");
    }
});

Unfortunately, that's not quite how things work. beforeModel isn't simply called before model is set, and afterModel afterward. They are just hooks that are called in that sequence ( beforeModel -> model -> afterModel ) as part of the lifecycle of a route.

Unfortunately, I haven't found a good way to unit tests Routes. If you have specific beforeModel logic that you need to test, then maybe you can just call beforeModel directly? I have found that Route logic is best tested through acceptance-style tests, since you then have the Route being invoked in the same way it will be when the application is actually running.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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