Angularjs 使用 jasmine 和 karma 测试 typescript ang angular 时出错
Angularjs error while testing typescript ang angular with jasmine and karma
我想用 Karma 和 Jasmine 测试我的 AngularJS 和基于 TypeScript 的应用程序。
我编辑的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
forEach@PROJECT_PATH/Scripts/angular.js:336:11
loadModules@PROJECT_PATH/Scripts/angular.js:4372:5
createInjector@PROJECT_PATH/Scripts/angular.js:4297:11
workFn@PROJECT_PATH/Scripts/angular-mocks.js:2427:44
env.executeFiltered@PROJECT_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/this.loaded@http://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)
如果我从我的代码中删除注入部分,它 运行 会成功并且测试通过。谁能帮我看看我做错了什么?
我们应该请求服务 $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);
}));
我想用 Karma 和 Jasmine 测试我的 AngularJS 和基于 TypeScript 的应用程序。
我编辑的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
forEach@PROJECT_PATH/Scripts/angular.js:336:11
loadModules@PROJECT_PATH/Scripts/angular.js:4372:5
createInjector@PROJECT_PATH/Scripts/angular.js:4297:11
workFn@PROJECT_PATH/Scripts/angular-mocks.js:2427:44
env.executeFiltered@PROJECT_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/this.loaded@http://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)
如果我从我的代码中删除注入部分,它 运行 会成功并且测试通过。谁能帮我看看我做错了什么?
我们应该请求服务 $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);
}));