lundi 1 août 2016

AngularJS router unit test failed

This is my router

angular.module('ps.auth')
  .config(function ($stateProvider) {
    $stateProvider.state('login', {
      url: '/login',
      views: {
        'content': {
          templateUrl: 'auth/login.html',
          controller: 'AuthCtrl'
        }
      },
      authNotRequired: true
    })
    .state('logout', {
        url: '/logout',
        controller: 'LogoutCtrl',
        authNotRequired: true
    });
  });

And this my router unit test

'use strict';
describe('ps.authe', function () {
  var $state, $stateParams, $q, $templateCache, $location, $rootScope, $injector;

  function mockTemplate(templateRoute, tmpl) {
    $templateCache.put(templateRoute, tmpl || templateRoute);
  }

  function goTo(url) {
    console.log('************** goTo url = ', url);
    $location.url(url);
    $rootScope.$digest();
  }

  beforeEach(inject(function (_$state_, _$stateParams_, _$q_, _$templateCache_, _$location_, _$rootScope_, _$injector_) {
    $state = _$state_;
    $stateParams = _$stateParams_;
    $q = _$q_;
    $templateCache = _$templateCache_;
    $location = _$location_;
    $rootScope = _$rootScope_;
    $injector = _$injector_;
  }));

  describe('path', function () {
    function goTo(url) {
      $location.url(url);
      $rootScope.$digest();
    }

    describe('/login', function () {
      beforeEach(mockTemplate.bind(null, 'auth/login.html'));
      it('should go to the login state', function () {
        console.log('****** This is $state ', $state);
        goTo('/login');
        expect($state.current.name).toEqual('login');
      });

    });
  });
});

$state is always undefined and I don't know why it is null for $state Here is the error message;

Running "karma:unit" (karma) task 01 08 2016 15:06:27.796:INFO [karma]: Karma v0.13.22 server started at http://localhost:8080/ 01 08 2016 15:06:27.804:INFO [launcher]: Starting browser PhantomJS 01 08 2016 15:06:28.462:INFO [PhantomJS 2.1.1 (Mac OS X 0.0.0)]: Connected on socket /#sgfs-LmGMtQWeachAAAA with id 55382905 LOG: '****** This is $state ', undefined PhantomJS 2.1.1 (Mac OS X 0.0.0) ps.authentication path /login should go to the login state FAILED Error: [$injector:unpr] Unknown provider: $stateProvider <- $state http://ift.tt/2aN7ZMO$injector/unpr?p0=%24stateProvider%20%3C-%20%24state (line 4031) /Users/user/git/pws/UI/myProject/bower_components/angular/angular.js:4031:86 getService@/Users/user/git/pws/UI/myProject/bower_components/angular/angular.js:4178:46 /Users/user/git/pws/UI/myProject/bower_components/angular/angular.js:4036:48 getService@/Users/user/git/pws/UI/myProject/bower_components/angular/angular.js:4178:46 invoke@/Users/user/git/pws/UI/myProject/bower_components/angular/angular.js:4210:23 workFn@/Users/user/git/pws/UI/myProject/bower_components/angular-mocks/angular-mocks.js:2437:26 undefined TypeError: undefined is not an object (evaluating '$templateCache.put') in /Users/user/git/pws/UI/myProject/app/auth/auth.route.spec.js (line 6) mockTemplate@/Users/user/git/pws/UI/myProject/app/auth/auth.route.spec.js:6:19 mockTemplate@[native code] TypeError: undefined is not an object (evaluating '$location.url') in /Users/user/git/pws/UI/myProject/app/auth/auth.route.spec.js (line 27) goTo@/Users/user/git/pws/UI/myProject/app/auth/auth.route.spec.js:27:16 /Users/user/git/pws/UI/myProject/app/auth/auth.route.spec.js:35:13 PhantomJS 2.1.1 (Mac OS X 0.0.0): Executed 133 of 133 (1 FAILED) (0.177 secs / 0.202 secs)

Any idea what was wrong in the test script -K

Aucun commentaire:

Enregistrer un commentaire