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

I can't seem to get the model hooks and the actions called with unit test.

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

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

but route.model () throws errors like: the transition is not 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?"); }); 

Route Example

 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"); } }); 
+7
javascript unit-testing ember-cli
source share
1 answer

Unfortunately, this is not quite how everything works. beforeModel not just called before installing model , and then afterModel . These are just hooks that are called in this sequence ( beforeModel -> model -> afterModel ) as part of the route’s lifecycle.

Unfortunately, I did not find a good way to unit test routes. If you have a specific beforeModel logic to test, maybe you can just call beforeModel directly? I found that the logic of the route is best checked using acceptance style tests, since then you call the Route in the same way as it will when you start the application.

+1
source share

All Articles