AngularJS Karma Jasmine:应定义未定义



我是AngularJS的新手,正在Karma和Jasmine中添加单元测试。我有一个下面的控制器代码,我正在尝试测试它的方法。我收到一个错误,上面写着Expected undefined to be defined。我不确定为什么SpyOn无法识别该方法。感谢您的帮助。提前谢谢。

newclaim.js

(function () {
'use strict';
var controllerId = 'createnewclaim';
angular.module('app').controller(controllerId, ['$location', '$routeParams', 'sessionKeys', 'securelogins',
'localStorageService', 'common', 'config', 'datacontext', createnewclaim]);
function createnewclaim($location, $routeParams, sessionKeys, securelogins, localStorageService, common, config, datacontext) {

//some code here

我的测试文件代码

describe('testing AngularJS Test Suite', function () {
beforeEach(module('app'));
describe('Testing AngularJS NewClaim Controller', function () {
var scope = {};
var ctrl;
var controllerId = 'createnewclaim';
beforeEach(inject(function ($controller, $rootScope, localStorageService, common, datacontext, sessionKeys, securelogins) {
scope = $rootScope.$new();

ctrl = function () {
$controller(controllerId, {
$scope: scope,
localStorageService: localStorageService,
common: common,
datacontext: datacontext,
sessionKeys: sessionKeys,
securelogins: securelogins
});
};
}));
it('controller defined', inject(function () {
expect(ctrl).toBeTruthy();
}));
it('controller function defined', inject( function () {
ctrl();
expect(ctrl.createnewclaim).toBeDefined();
}));

});
});
})();

对不起。忽略退货。您需要使用scope作为测试,而不是ctrl。

describe('testing AngularJS Test Suite', function () {
beforeEach(module('app'));
describe('Testing AngularJS NewClaim Controller', function () {
var scope = {};
var ctrl;
var controllerId = 'createnewclaim';
beforeEach(inject(function ($controller, $rootScope, localStorageService, common, datacontext, sessionKeys, securelogins) {
scope = $rootScope.$new();

ctrl = function () {
return $controller(controllerId, {
$scope: scope,
localStorageService: localStorageService,
common: common,
datacontext: datacontext,
sessionKeys: sessionKeys,
securelogins: securelogins
});
};
}));
it('controller defined', inject(function () {
ctrl();
expect(scope).toBeTruthy();
}));
it('controller function defined', inject( function () {
ctrl();
expect(scope.createnewclaim).toBeDefined();
}));            
});
});
})();