mardi 2 août 2016

Angular ui router unit testing failed

I have some issues with my unit test for router here is my router

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

Here is unit test for router

'use strict';
describe('ps.auth/login', function () {
  var $rootScope, $state, $injector, state = 'login', myUserName='userName';

  beforeEach(module('ps.auth'));
  beforeEach(module('ui.router'));

  beforeEach(function() {
    inject(function(_$rootScope_, _$state_, _$injector_, $templateCache) {
      $rootScope = _$rootScope_;
      $state = _$state_;
      $injector = _$injector_;

      // We need add the template entry into the templateCache if we ever
      // specify a auth/login.html
      $templateCache.put('auth/login.html', '');
       $state.go('login');
       $state.$apply();
    });
  });

  it('should respond to URL', function() {
    console.log('********** $state = ', $state);
    expect($state.href(state, { username: myUserName })).toEqual('#/login/userName');
  });


});

I got the error message TypeError: undefined is not an object (evaluating '$state.href') because $state is undefined.

Any suggestion how to fix this issue.

Aucun commentaire:

Enregistrer un commentaire