【发布时间】:2013-11-21 21:58:13
【问题描述】:
我正在尝试开始使用 Karma 测试,将它们添加到现有的 Angular 应用程序中。
这是我的主要应用定义文件:
angular
.module('myApp', [
'ngRoute',
'moduleAdherence'
]);
这是我的控制器文件:
angular
.module('moduleAdherence', [])
.controller('AdherenceCtrl', ['$scope', function ($scope) {
$scope.awesomeThings = [1,2,3,4];
}]);
这是我第一次尝试文件:
describe('Controller: AdherenceCtrl', function () {
beforeEach(module('myApp'));
var MainCtrl,
scope;
beforeEach(inject(function ($controller, $rootScope) {
scope = $rootScope.$new();
MainCtrl = $controller('AdherenceCtrl', {
$scope: scope
});
}));
it('should attach a list of awesomeThings to the scope', function () {
expect(scope.awesomeThings.length).toBe(4);
});
});
当我尝试使用grunt test 运行它时,它失败并出现以下错误:
Uncaught Error: [$injector:nomod] Module 'd3' is not available!
You either misspelled the module name or forgot to load it.
If registering a module ensure that you specify the dependencies
as the second argument.
http://errors.angularjs.org/1.2.0/$injector/nomod?p0=d3
at /Users/me/Dropbox/projects/myapp/app/bower_components/angular/angular.js:1498
我不明白这一点,因为这个控制器不使用 D3。我确实在应用程序的其他地方的指令中使用了 D3,但我没有在模块中注册它(我使用外部 D3 文件)。
为什么 Karma 会注意到 D3?是不是应该不用D3也能测试这个控制器?
【问题讨论】:
-
模块
myApp在哪里定义?还有MainCtrl? -
抱歉 - 重命名以供公众使用时的拼写错误,现已修复。问题还是一样。
-
想通了 - 我必须在
karma.config.js的files部分中显式加载依赖项。 -
@Richard 很好,请添加它作为解决方案!
-
使用
karma-angular-filesort
标签: javascript angularjs karma-runner