2015-09-29 55 views
1

我想測試我的基於AngularJS和基於TypeScript的應用程序與Karma和茉莉花。Angularjs錯誤,而測試打字稿角與茉莉花和業力

我的編輯controllerSpecs.ts

/// <reference path="../references.ts" /> 

describe('ConfigCtrl',() => { 
var configCtrl, scope; 

beforeEach(angular.mock.module('typewritingApp')); 

beforeEach(angular.mock.inject(($rootScope) => { 
    scope = $rootScope.$new(); 
    configCtrl = new App.TestCtrl(scope); 
})); 

it('should be true',() => { 
    expect(true).toBe(true); 
}); 
}); 

app.ts

module App { 
import TypewritingCtrl = App.TypewritingCtrl; 
import MenuCtrl = App.MenuCtrl; 
var typescrip: ng.IModule; 
var typewritingapp: ng.IModule = angular.module("typewritingApp", ["ngRoute", "ui.bootstrap", "LocalStorageModule", "ui.event", "timer"]) 
    .controller("TestCtrl", ["$scope", TestCtrl]) 

} 

TestController.ts

/// <reference path="../references.ts" /> 
module App { 
export class TestCtrl { 
    constructor($scope: ng.IRootScopeService) { 

    } 
} 
} 

和錯誤是什麼因緣寫我,當我試圖運行測試:

Firefox 41.0.0 (Windows 10 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.089 secs/0.119 secs) 
29 09 2015 14:58:55.573:INFO [watcher]: Changed file "PROJECT_PATH/Specs/controllerSpecs.js". 
Firefox 41.0.0 (Windows 10 0.0.0) ConfigCtrl should be true FAILED 
    minErr/<@PROJECT_PATH/Scripts/angular.js:68:12 
    loadModules/<@PROJECT_PATH/Scripts/angular.js:4411:15 
    [email protected]_PATH/Scripts/angular.js:336:11 
    [email protected]_PATH/Scripts/angular.js:4372:5 
    [email protected]_PATH/Scripts/angular.js:4297:11 
    [email protected]_PATH/Scripts/angular-mocks.js:2427:44 
    [email protected]_PATH/Specs/node_modules/karma-jasmine/lib/boot.js:117:7 
    createStartFn/<@PROJECT_PATH/Specs/node_modules/karma-jasmine/lib/adapter.js:171:5 
    [2]</Karma/[email protected]://localhost:9876/karma.js:190:7 
    @http://localhost:9876/context.html:52:5 

Firefox 41.0.0 (Windows 10 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.255 secs/0.103 secs) 

如果我從我的代碼中刪除注入部分,它會成功運行並通過測試。任何人都可以幫助我,我做錯了什麼?

回答

0

我們應該問的服務$rootScope作爲本地$scope發生器:

// beforeEach(angular.mock.inject(($scope) => { 
beforeEach(angular.mock.inject(($rootScope) => 
{ 
    // HERE we do create a $scope from $rootScope 
    scope = $rootScope.$new(); 
    configCtrl = new App.TestCtrl(scope); 
})); 
+0

感謝您的回答@RadimKöhler,我已經改變了代碼,但仍收到了同樣的錯誤。 – bucicimaci

+0

我在本地複製了你的代碼,它正在爲我工​​作**,因爲它是**。我簡化了模塊'.module(「typewritingApp」,[])'並實現了空的'TestCtrl'。測試正在通過... –

+0

然後我不明白。我已經通過NuGet Package Manager [this]添加了所有de js和d.ts文件(https://blogs.endjin.com/2014/09/unit-testing-angularjs-with-visual-studio-resharper-and-teamcity /)例子也很好。這就是我的業力配置看起來像[鏈接](http://pastebin.com/TUvvGxj2) – bucicimaci