unit test for AngularJS router

24 views
Skip to first unread message

Kim Phan

unread,
Aug 3, 2016, 2:19:20 PM8/3/16
to Jasmine
Hi all,
I have the following router
Enter code here. 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
    });
  });
..

and here is my unit test for this router
Enter code here...'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  auth/login.html
      $templateCache.put('auth/login.html', '');
       $state.go('login');
       $state.$apply();
    });
  });

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

});

I got the error message : TypeError: Cannot read property 'href' of undefined
but don't know how to fix it. Please help

Reply all
Reply to author
Forward
0 new messages