运行 angular 使用 karma 和 jasmine 进行测试:minErr 错误
Running angular tests with karma and jasmine: minErr error
我正在尝试为 angular 控制器编写基本测试。我使用 Jasmine 框架和业力作为测试运行器。我的测试失败并出现以下错误:
Firefox 35.0.0 (Linux) MessageCtrl should get the correct message FAILED
minErr/<@/home/jenselme/tests/javascript/angular/unit_tests/angular.js:78:12
loadModules/<@/home/jenselme/tests/javascript/angular/unit_tests/angular.js:3906:15
forEach@/home/jenselme/tests/javascript/angular/unit_tests/angular.js:325:9
loadModules@/home/jenselme/tests/javascript/angular/unit_tests/angular.js:3872:5
createInjector@/home/jenselme/tests/javascript/angular/unit_tests/angular.js:3812:11
workFn@/home/jenselme/tests/javascript/angular/unit_tests/angular-mocks.js:2172:44
env.executeFiltered@/home/jenselme/.npm-packages/lib/node_modules/karma-jasmine/lib/boot.js:126:7
createStartFn/<@/home/jenselme/.npm-packages/lib/node_modules/karma-jasmine/lib/adapter.js:171:5
[2]</Karma/this.loaded@http://localhost:9877/karma.js:185:7
@http://localhost:9877/context.html:47:5
TypeError: $scope is undefined in /home/jenselme/tests/javascript/angular/unit_tests/MessageCtrl.spec.js (line 11)
@/home/jenselme/tests/javascript/angular/unit_tests/MessageCtrl.spec.js:11:5
env.executeFiltered@/home/jenselme/.npm-packages/lib/node_modules/karma-jasmine/lib/boot.js:126:7
createStartFn/<@/home/jenselme/.npm-packages/lib/node_modules/karma-jasmine/lib/adapter.js:171:5
[2]</Karma/this.loaded@http://localhost:9877/karma.js:185:7
@http://localhost:9877/context.html:47:5
这是我的 MessageCtrl.js:
var app = angular.module("lastLine", []);
app.controller('MessageCtrl', ['$scope', function($scope) {
$scope.getMessage = function(name) {
return 'Hello ' + name;
};
}]);
还有我的MessageCtrl.spec.js:
describe('MessageCtrl', function() {
var $scope;
beforeEach(module('MessageCtrl'));
beforeEach(inject(function($rootScope, $controller) {
$scope = $rootScope.$new();
$controller('MessageCtrl', {
$scope: $scope
});
}));
it('should get the correct message', function() {
var message = $scope.getMessage('Cedric');
expect(message).toBe('Hello Cedric');
})
});
我不知道那可能来自哪里。谁能帮帮我?
编辑 1:更正 MessageCtrl.spec.js
的代码
知道了。 MessageCtrl.spec.js 中的代码是错误的。 beforeEach(module('MessageCtrl'));
行应该是 beforeEach(module('lastLine'));
我正在尝试为 angular 控制器编写基本测试。我使用 Jasmine 框架和业力作为测试运行器。我的测试失败并出现以下错误:
Firefox 35.0.0 (Linux) MessageCtrl should get the correct message FAILED
minErr/<@/home/jenselme/tests/javascript/angular/unit_tests/angular.js:78:12
loadModules/<@/home/jenselme/tests/javascript/angular/unit_tests/angular.js:3906:15
forEach@/home/jenselme/tests/javascript/angular/unit_tests/angular.js:325:9
loadModules@/home/jenselme/tests/javascript/angular/unit_tests/angular.js:3872:5
createInjector@/home/jenselme/tests/javascript/angular/unit_tests/angular.js:3812:11
workFn@/home/jenselme/tests/javascript/angular/unit_tests/angular-mocks.js:2172:44
env.executeFiltered@/home/jenselme/.npm-packages/lib/node_modules/karma-jasmine/lib/boot.js:126:7
createStartFn/<@/home/jenselme/.npm-packages/lib/node_modules/karma-jasmine/lib/adapter.js:171:5
[2]</Karma/this.loaded@http://localhost:9877/karma.js:185:7
@http://localhost:9877/context.html:47:5
TypeError: $scope is undefined in /home/jenselme/tests/javascript/angular/unit_tests/MessageCtrl.spec.js (line 11)
@/home/jenselme/tests/javascript/angular/unit_tests/MessageCtrl.spec.js:11:5
env.executeFiltered@/home/jenselme/.npm-packages/lib/node_modules/karma-jasmine/lib/boot.js:126:7
createStartFn/<@/home/jenselme/.npm-packages/lib/node_modules/karma-jasmine/lib/adapter.js:171:5
[2]</Karma/this.loaded@http://localhost:9877/karma.js:185:7
@http://localhost:9877/context.html:47:5
这是我的 MessageCtrl.js:
var app = angular.module("lastLine", []);
app.controller('MessageCtrl', ['$scope', function($scope) {
$scope.getMessage = function(name) {
return 'Hello ' + name;
};
}]);
还有我的MessageCtrl.spec.js:
describe('MessageCtrl', function() {
var $scope;
beforeEach(module('MessageCtrl'));
beforeEach(inject(function($rootScope, $controller) {
$scope = $rootScope.$new();
$controller('MessageCtrl', {
$scope: $scope
});
}));
it('should get the correct message', function() {
var message = $scope.getMessage('Cedric');
expect(message).toBe('Hello Cedric');
})
});
我不知道那可能来自哪里。谁能帮帮我?
编辑 1:更正 MessageCtrl.spec.js
的代码知道了。 MessageCtrl.spec.js 中的代码是错误的。 beforeEach(module('MessageCtrl'));
行应该是 beforeEach(module('lastLine'));