1

我很难弄清楚这一点。我开始构建我的测试脚本。虽然有点晚了。但是单元测试是一种很好的做法。首先,我只想测试两个范围模型(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 is not function。请帮忙。

4

1 回答 1

2

它是否正确?

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

似乎该模块应该命名为 mainCtrl 以外的名称,因为这是您的控制器的名称。

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

beforeEach(function(){
  module('Services.api'),
  module('LocalStorageModule')
});
于 2013-09-11T14:39:15.547 回答