Jasmine Testing Angular 1.5 $ state.go

时间:2017-03-15 10:32:01

标签: angularjs jasmine

我有以下控制器,它具有根据状态重定向的功能。

(function() {

angular.module('CL.Authenticate')
    .controller('AuthenticateController', AuthenticateController);

AuthenticateController.$inject = ['$window', '$log', '$state', 'authenticationService', 'identity', 'UserService', 'MessagesService', 'ModalsService', '$location'];
function AuthenticateController($window, $log, $state, authenticationService, identity, userService, MessagesService, ModalsService, $location) {
    var vm = this;.......

function navigateToLoggedInState() {
        userService.getCurrentOrganisation().then(function(organisation) {
            if ($state.params.redirectUrl) {
                $location.path($state.params.redirectUrl);
            } else if (organisation.IsSupplier) {
                $state.go('dashboard.home.supplier');
            } else (organisation.IsBuyer) {
                $state.go('dashboard.home.buyer');
            }
        });
    }....

状态机在配置文件中声明:

(function () {
'use strict';

angular.module('CL.Dashboard').config(['$stateProvider', 'DashboardConstants', dashboardConfig])
    .run(['$rootScope', '$state', 'UserService', function($rootScope, $state, userService){
        $rootScope.$on('$stateChangeStart', function(event, toState){
            if (toState.name === 'dashboard.home'){
                event.preventDefault();
                userService.getCurrentOrganisation().then(function(organisation){
                    if (organisation.IsSupplier){
                        $state.go('dashboard.home.supplier');
                    } else (organisation.IsBuyer) {
                        $state.go('dashboard.home.buyer');
                    } 
                });
            }
        });
    }]);
function dashboardConfig ($stateProvider, DashboardConstants) {
    $stateProvider
        .state('dashboard.home', {
            url: '/',
            templateUrl: 'dashboard/home.html',
            controller: 'DashboardController',
            controllerAs: 'vm',
            resolve: {
                pasStatus: ['DashboardService', function(DashboardService){
                    return DashboardService.getPasStatus();
                }]
            }
        })
        .state('dashboard.home.buyer', {
            url: 'buyer',
            templateUrl: 'dashboard/buyer/buyer.html',
            controller: 'DashboardBuyerController',
            controllerAs: 'vm'
        }).....

在我的spec.js文件中,我有:

describe('Authenticate Controller', function () {

beforeEach(module('CL.Authenticate'));
beforeEach(module('CL.Users'));
beforeEach(module('CL.Dashboard'));

describe('authenticate controller', function () {

    var $log, $scope, $state, authenticationService, controller, userService, ModalsService, $stateProvider;;

    beforeEach(inject(function ($rootScope, _$state_, $controller, _$log_, $injector, $q, _identity_, _UserService_) {
        $log = _$log_;
        $scope = $rootScope.$new();
        $state = _$state_;
        userService = _UserService_;


        authenticationService = $injector.get('authenticationService');
        controller = $controller('AuthenticateController', {$scope: $scope, $log: $log, identity: _identity_, userService: userService, ModalsService: ModalsService});

        spyOn(authenticationService, "authenticate").and.callFake(function (emailAddress, password) {
.......
expect(authenticationService.authenticate).toHaveBeenCalledWith('test@test.com', 'password123', true, { 401: false });

在expect语句中作为authenticate方法的一部分,调用navigateToLoggedInState,当行 $ state.go('dashboard.home.supplier'); 被命中时,我收到以下错误:

无法从州''解析'dashboard.home.supplier'     在Object.transitionTo(angular-ui-router.js:3179)

我的状态设置错过了什么?这似乎与@tanmay确定的答案没有重复,因为我在测试中没有直接调用$ state。然而,我尝试创建一个新文件,并按照给出的示例,但我仍然收到相同的错误。

1 个答案:

答案 0 :(得分:0)

我仍然认为解决方案只在于that answer。让我列出你需要做的所有事情。

  • this gist
  • 获取stateMock模块
  • 通过在您的业力单元测试文件中包含stateMock模块的JS文件,确保它可用
  • 模块应该通过beforeEach(module('stateMock'));
  • 提供
  • 将其注入您的规范

    beforeEach(inject(function ($rootScope, $state, $controller, _$log_, 
    $injector, $q, _identity_, _UserService_) {
      $log = _$log_;
      $scope = $rootScope.$new();
      state = $state;
      userService = _UserService_;
      ...
    
  • 现在在您的规范中,在调用控制器功能之前,添加expectTransitionTo,如下所示:

    state.expectTransitionTo('dashboard.home.supplier');
    

最后一点有点棘手,因为我们通常倾向于在实际函数调用之后放置与expect相关的任何内容:)

希望它有效!