【发布时间】:2014-07-17 08:31:46
【问题描述】:
我正在尝试使用 Karma 测试我的应用程序,但出现以下错误:
minErr/<@/home/usr/webui-ng/src/client/app/bower_components/angular/angular.js:78:5
loadModules/<@/home/usr/webui-ng/src/client/app/bower_components/angular/angular.js:3859:1
forEach@/home/usr/webui-ng/src/client/app/bower_components/angular/angular.js:325:7
loadModules@/home/usr/webui-ng/src/client/app/bower_components/angular/angular.js:3824:5
createInjector@/home/usr/webui-ng/src/client/app/bower_components/angular/angular.js:3764:3
workFn@/home/usr/webui-ng/src/client/app/bower_components/angular-mocks/angular-mocks.js:2150:9
这些是我的文件:
你好.js
angular.module('myApp', [])
.controller('MainController', function($scope) {
$scope.name = "Ari";
$scope.sayHello = function() {
$scope.greeting = "Hello " + $scope.name;
}
})
hello.js - 测试文件:
describe('Unit: MainController', function() {
// Load the module with MainController
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('MainController', {
$scope: scope
});
}));
it('should create $scope.greeting when calling sayHello',
function() {
expect(scope.greeting).toBeUndefined();
scope.sayHello();
expect(scope.greeting).toEqual("Hello Ari");
});
});
如您所见,我已经加载了模块,因为解决方案在 Controller undeclared in Jasmine test 中。
还能是什么?我在网上没有找到太多关于这些错误的信息。我很高兴最终找到答案。
【问题讨论】:
-
您使用的 angular.js 版本是什么?
-
请确保 hello.js(真正的代码)确实包含在 karma.conf.js 中
-
是的,这解决了它!非常感谢!
标签: javascript angularjs unit-testing controller karma-runner