我正在轻松地遵循本指南 - http://paislee.io/testing-angularjs-with-grunt-karma-and-jasmine/ - 并有以下一些问题:
Error: [$injector:modulerr] Failed to instantiate module myApp due to:
Error: [$injector:modulerr] Failed to instantiate module ngRoute due to:
Error: [$injector:nomod] Module 'ngRoute' 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.
我按照它告诉我的那样安装了所有内容,并找到了另一个非常基本的设置测试的示例(这是我第一次实现,所以我从小处开始),测试看起来像这样
describe('Unit: MainCtrl', function() {
// Load the module with MainCtrl
beforeEach(module('myApp'));
var ctrl, scope;
// inject the $controller and $rootScope services
// in the beforeEach block
beforeEach(inject(function($controller, $rootScope) {
// Create a new scope that's a child of the $rootScope
scope = $rootScope.$new();
// Create the controller
ctrl = $controller('MainCtrl', {
$scope: scope
});
}));
it('should create $scope.greeting when calling sayHello',
function() {
expect(scope.greeting).toBeUndefined();
scope.sayHello();
expect(scope.greeting).toEqual("Hello Ari");
});
})
在 Controller 中它只是
$scope.name = "Ari";
$scope.sayHello = function() {
$scope.greeting = "Hello " + $scope.name;
}
(来自 http://www.ng-newsletter.com/advent2013/#!/day/19 )
我使用常规的 ng-route 结构将应用程序和 Controller 设置在单独的文件夹中,我想这可能是问题所在?我为此使用了 grunt karma - 这是任务,以防万一它有帮助。
karma: {
unit: {
options: {
frameworks: ['jasmine'],
singleRun: true,
browsers: ['PhantomJS'],
files: [
'app/bower_components/angular/angular.js',
'app/bower_components/angular-mocks/angular-mocks.js',
'app/scripts/**/*.js'
]
}
}
}
我需要一些帮助,这是我第一次尝试这样做,我希望进行一些自动化测试。感谢您的阅读!!
最佳答案
您需要将 ngRoute 包含在 karma conf 的文件列表中。错误消息说明了这一点。
karma: {
unit: {
options: {
frameworks: ['jasmine'],
singleRun: true,
browsers: ['PhantomJS'],
files: [
'app/bower_components/angular/angular.js',
'app/bower_components/angular-mocks/angular-mocks.js',
'app/bower_components/angular-mocks/angular-route.js',
'app/scripts/**/*.js'
]
}
}
}
关于javascript - 为 Angular 设置 Jasmine/karma,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28184483/