mercredi 23 septembre 2015

Unit tests break after adding a run block in application

I am facing a problem where all my unit tests are failing after adding a run block to handle the $stateChangeError event from a configured ui-router in my application.

The tests all fail for the same reason, as an unexpected request is made to a page which gets loaded from the default state in my ui-router config.

Commenting out the run block allows all the tests to pass again.

When the app runs, the $stateChangeError event handling does work as expected.

So how can I deal with this run block in my tests? I don't want to be adding an extra expectGET for the page from my default route across all my tests if I can help it - unless I have no choice?

I did come across this post which talks about moving the run block into another module to stop it interfering with tests.

angular.module('app', ['ui.router', 'restangular', 'ui.bootstrap']);

angular.module('app-start', ['app']).run(function($rootScope, $state) {
  return $rootScope.$on('$stateChangeError', function(event, toState, toParams, fromState, fromParams, error) {
    return $state.reload();
  });
});

I tried this and my tests passed, but when the app runs, the $stateChangeError event handling I have added no longer works.

Can any one give me some guidance? Thanks

Aucun commentaire:

Enregistrer un commentaire