Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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");
    }
});
like image 646
Stuarty Avatar asked Oct 15 '14 13:10

Stuarty


1 Answers

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.

like image 63
Alex LaFroscia Avatar answered Oct 06 '22 01:10

Alex LaFroscia