【问题标题】:AngularJs test throwing exception with Jasmine via KarmaAngularJs 通过 Karma 测试 Jasmine 抛出异常
【发布时间】:2014-12-07 01:08:53
【问题描述】:

我在测试我用 TypeScript 编写的有角度的东西时遇到了一个奇怪的异常,这是我的两个文件:

文件名:routeStates.ts

module Application {
    'use strict';

    class RootState implements ng.ui.IState {
        name: string;
        url: string;
        template: string;
        'abstract': boolean;

        constructor(settings: ISettings) {
            this.name = 'portal';
            this.url = '';
            this.template = '<div ui-view></div>';
            this.abstract = true;
        }
    }

    class ReportsState implements ng.ui.IState {
        name: string;
        url: string;
        templateUrl: string;
        'abstract': boolean;

        constructor(settings: ISettings) {
            this.name = 'portal.reports';
            this.url = '/reports';
            this.templateUrl = settings.partialsPath + 'reports/partial-reports.html';
            this.abstract = true;
        }
    }

    class AdminState implements ng.ui.IState {
        name: string;
        url: string;
        templateUrl: string;
        'abstract': boolean;

        constructor(settings: ISettings) {
            this.name = 'portal.admin';
            this.url = '/admin';
            this.templateUrl = settings.partialsPath + 'admin/partial-admin.html';
            this.abstract = true;
        }
    }

    export class ApplicationRoutingConfig {
        public static configure(
            $stateProvider: ng.ui.IStateProvider,
            $urlRouteProvider: ng.ui.IUrlRouterProvider,
            settings: ISettings) {
            $urlRouteProvider.otherwise('/reports/generate-reports');
            $stateProvider
                .state(new RootState(settings))
                .state(new ReportsState(settings))
                .state(new AdminState(settings));
        }
    }
}

文件名:routeStatesTests.ts

/// <reference path="../../lib/jasmine/jasmine.d.ts" />
module App {
    'use strict';

    describe('Application Route States Tests', () => {

        beforeEach(() => {
            module('ui.router');
            module('Application');
        });

        //beforeEach(module('Application'));


        it('should have a ApplicationRoutingConfig controller', () => {
            expect(Application.ApplicationRoutingConfig).toBeDefined();
        });

        it('should initialize ApplicationRoutingConfig', () => {
            expect(Application.ApplicationRoutingConfig.configure).toBeDefined();
        });

        var $stateProvider: ng.ui.IStateProvider;
        var $urlRouterProvider: ng.ui.IUrlRouterProvider;
        var settings: Application.ISettings = new Application.Settings();

        //This is throwing exception 

        it('should not return empty config', () => {
            expect(Application.ApplicationRoutingConfig.configure($stateProvider, $urlRouterProvider, settings)).toNotEqual(null);
        });

    });
}

我遇到了异常:

TypeError: Unable to get property 'otherwise' of undefined or null reference at ApplicationRoutingConfig.configure 

除此之外,所有其他测试都已通过。

对此的任何帮助将不胜感激!

【问题讨论】:

    标签: angularjs tdd typescript jasmine bdd


    【解决方案1】:

    您错过了注入依赖项...

     module Application {
                ...
                export class ApplicationRoutingConfig {
                    public static configure(
                        $stateProvider: ng.ui.IStateProvider,
                        $urlRouteProvider: ng.ui.IUrlRouterProvider,
                        settings: ISettings) {
                        $urlRouteProvider
                            .otherwise('/reports/generate-reports');
                        $stateProvider
                            .state(new RootState(settings))
                            .state(new ReportsState(settings))
                            .state(new AdminState(settings));
                        //Missing?: should not return empty config?
                        return this; //?
        }}}
    and then in routetStateTest
    
        describe('Application Route States Tests', () => {
    
            var urlRouterProvider: ng.ui.IUrlRouterProvider;
            var stateProvider: ng.ui.IStateProvider;
            var settings: Application.ISettings = new Application.Settings();
    
              beforeEach(() => {
                   module('ui.router');
                });
    
               //missing: instantiating/injecting
                   beforeEach(module(function($stateProvider, $urlRouterProvider) {
                       urlRouterProvider = $urlRouterProvider;
                       stateProvider = $stateProvider;
                   }));
    
              it('should have a ApplicationRoutingConfig controller', () => {
                  expect(Application.ApplicationRoutingConfig).toBeDefined();
              });
    
              it('should initialize ApplicationRoutingConfig', () => {
                  expect(Application.ApplicationRoutingConfig.configure)
                  .toBeDefined();
              });
    
              //missing: instantiating/injecting
              it('should not return empty config', 
                  inject(() => {
                      expect(Application
                          .ApplicationRoutingConfig
                          .configure(stateProvider, urlRouterProvider, settings)
                      ).toNotEqual(null);
                  }
              ));
    Karma: 
    SUCCESS myApp.version module interpolate filter should replace VERSION
    SUCCESS myApp.version module app-version directive should print current version
    SUCCESS myApp.version module version service should return current version
    SUCCESS myApp.view1 module view1 controller should ....
    SUCCESS myApp.view2 module view2 controller should ....
    SUCCESS Application Route States Tests should have a ApplicationRoutingConfig controller
    SUCCESS Application Route States Tests should initialize ApplicationRoutingConfig
    SUCCESS Application Route States Tests should not return empty config
    

    【讨论】:

    • Dan- 它有很大帮助,但我注意到 routeStates.ts 没有返回空配置,请看这里是编译后的 JS 代码:
    • var ApplicationRoutingConfig = (function () { function ApplicationRoutingConfig() { } ApplicationRoutingConfig.configure = function ($stateProvider, $urlRouteProvider, settings) { $urlRouteProvider.otherwise('/reports/generate-reports'); $stateProvider.state(new RootState(settings)).state(new ReportsState(settings)).state(new AdminState(settings)); }; return ApplicationRoutingConfig; })(); Application.ApplicationRoutingConfig = ApplicationRoutingConfig;
    • 如果参考我的评论//Missing?: should not return empty config?,不确定你的意思是什么失败了,因为测试预期不为空
    • 我的坏事令人困惑return ApplicationRoutingConfig; 这是什么意思?不是返回配置吗?
    • function ApplicationRoutingConfig(){} 正在声明函数但尚未实例化,并且 assert 正在评估 configure($stateProvider, $urlRouteProvider, settings) 及其结果
    猜你喜欢
    • 2013-05-15
    • 2014-01-10
    • 2014-05-04
    • 1970-01-01
    • 2014-09-20
    • 2016-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多