2014-10-12 79 views
1
引發異常茉莉

我面臨着一個奇怪的例外,同時測試我寫在這裏打字稿角的東西都被我的文件:AngularJs測試通過噶

文件名: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); 
     }); 

    }); 
} 

我越來越excepti上:

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

所有其他測試越來越期待這一個。

任何幫助,將不勝感激!

回答

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 
+0

Dan-有很大幫助,但我注意到'routeStates.ts'沒有返回空的配置,在這裏看到的是編譯的JS代碼: – 2014-10-16 10:06:36

+0

'變種ApplicationRoutingConfig =(函數(){ 函數ApplicationRoutingConfig(){} ApplicationRoutingConfig.configure =函數($ stateProvider,$ urlRouteProvider,設置){$ urlRouteProvider.otherwise( '/報告/生成的報告'); $ stateProvider .state(新的RootState(設置))。state(新的ReportsState(設置))。state(新的AdminState (設置)); }; return ApplicationRoutingConfig; })(); Application.ApplicationRoutingConfig = ApplicationRoutingConfig;' – 2014-10-16 10:07:49

+0

不確定你的意思*注意到routeStates.ts沒有返回空配置*如果引用我的評論'//失蹤?:不應該返回空配置?'我的意思是函數wasn' t返回任何東西,測試失敗,因爲測試期望不是空的 – Dan 2014-10-16 10:53:19