错误:参数'Ctrl'不是函数,得到了未定义的单元测试

时间:2013-09-11 13:56:01

标签: unit-testing angularjs

我很难搞清楚这一点。我开始构建我的测试脚本。虽然它有点晚了。但单元测试是一种很好的做法。首先,我只想测试两个范围模型(scope.global& scope.security),但我得到一个奇怪的错误,说我的MainCtrl不是函数?

'use strict';

/* Controllers */

angular.module('mainCtrl', ['LocalStorageModule'])
.controller('MainCtrl', ['$scope' ,'$rootScope', '$location', 'Api', 'Security', 'Utils', 'localStorageService',function (scope, rootScope, location, Api, Security, Utils, session) {
    console.log('main js loaded');
    // $scope.main = {};
    scope.global = {};
    scope.security = Security;

    ....
}]);

我的controllerSpec.js

describe('controllers MainCtrl', function(){

beforeEach(function(){
module('Services.api'),
module('LocalStorageModule')
});

describe('MainCtrl', function() {

var scope, api, security, session;
beforeEach(inject(function($rootScope, $controller ,$location, Api, localStorageService){
    scope = $rootScope.$new();
    $location = $location;

    $controller("MainCtrl", {
        $scope : scope,
    localStorageService : localStorageService,
        Api : Api
    });
}));

it('should create "usertype" model with empty list', function(){
    expect(scope.global).toBe(undefined);   
});
  });
});

上述代码导致错误:

Chrome 24.0 (Linux) controllers MainCtrl MainCtrl should create "usertype" model with empty list FAILED
Error: Argument 'MainCtrl' is not a function, got undefined

我已经在浏览器上测试了我的webapp,它没有遇到这个MainCtrl不是功能。 请帮忙。

1 个答案:

答案 0 :(得分:2)

这是对的吗?

angular.module('mainCtrl', ['LocalStorageModule'])

似乎模块应该被命名为mainCtrl以外的东西,因为这是你的控制器命名的。

在测试中,您没有加载模块:

beforeEach(function(){
  module('Services.api'),
  module('LocalStorageModule')
});