Tet*_*yna 10 javascript jasmine angularjs karma-runner karma-jasmine
我试图测试我的控制器时遇到了问题.当我运行测试时出现错误
Error: [ng:areq] Argument 'MainCtrl' is not a function, got undefined http://errors.angularjs.org/1.3.8/ng/areq?p0=MainCtrl&p1=not%20a%20function%2C%20got%20undefined
at assertArg (/Users/tetianachupryna/project/bower_components/angular/angular.js:1577)
at assertArgFn (/Users/tetianachupryna/project/bower_components/angular/angular.js:1588)
at /Users/tetianachupryna/project/bower_components/angular/angular.js:8418
at /Users/tetianachupryna/project/src/spec/controllers/main-controller.spec.js:11
at /Users/tetianachupryna/project/src/spec/controllers/main-controller.spec.js:17
at /Users/tetianachupryna/project/node_modules/karma-jasmine/lib/adapter.js:184
at http://localhost:9877/karma.js:185
at http://localhost:9877/context.html:51
Run Code Online (Sandbox Code Playgroud)
我知道SO充满了类似的问题.但是我在Angular和JS中总体上是空的,所以这些答案对我没有帮助.从SO上的类似问题我发现我的问题是控制器的错误定义,但我仍然无法弄清楚我做错了什么.我已经堆叠了,我求你帮忙.
首先,这是我的src/app/index.js文件,其中定义了我的模块
var app = angular.module('myModule', [
'ngAnimate',
'ngSanitize',
'ngResource',
'ui.router',
'pascalprecht.translate',
'thing1',
'thing2']);
Run Code Online (Sandbox Code Playgroud)
这是src/app/controllers/main-controller.js
angular.module('myModule').controller('MainCtrl', [
'$scope',
'$state',
function ($scope, $state) {
$scope.state = $state;
//***
$scope.isBigStep = function isBigStep() {
return $state.$current.step == 3;
};
}]);
Run Code Online (Sandbox Code Playgroud)
最后这个文件带有测试src/spec/controllers/main-controller.spec.js
describe('MainCtrl', function() {
var scope, $state, createController;
beforeEach(inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
createController = function() {
return $controller('MainCtrl', {
'$scope': scope
});
};
}));
it('should make 3 as current step', function() {
var controller = createController();
expect(scope.isBigStep()).toBe(true);
});
});
Run Code Online (Sandbox Code Playgroud)
在karma配置中我拥有所有这些文件
files: [
'bower_components/angular/angular.js',
'bower_components/angular-mocks/angular-mocks.js',
'src/app/index.js',
'src/app/controllers/*.js',
'src/spec/controllers/*.js'
],
Run Code Online (Sandbox Code Playgroud)
为了运行我的测试,我使用RubyMine中的karma-runner插件.
我会感谢任何帮助!
PSL*_*PSL 14
您缺少的是在beforeEach挂钩中添加模块以进行测试设置.否则将找不到控制器.所以加beforeEach(module('myModule'))
.
describe('MainCtrl', function() {
var scope, $state, createController;
beforeEach(module('myModule')); //<--- Hook module
beforeEach(inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
createController = function() {
return $controller('MainCtrl', {
'$scope': scope
});
};
}));
it('should make 3 as current step', function() {
var controller = createController();
expect(scope.isBigStep()).toBe(true);
});
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10034 次 |
最近记录: |