以下是我的示例角度项目中的代码 app.js代码:
(function () {
'use strict';
angular.module('actorsDetails').controller('HomeCtrl', HomeCtrl);
/* @ngInject */
function HomeCtrl($state) {
/* jshint validthis: true */
var vm = this;
vm.activate = activate;
vm.test = true;
vm.navigate = navigate;
activate();
function activate() {
}
function navigate() {
$state.go('form');
}
}
})();
**test.js**
describe('HomeCtrl', function() {
beforeEach(module('actorsDetails'));
beforeEach(inject(function ($rootScope, $controller) {
var scope = $rootScope.$new();
var HomeCtrl = $controller('HomeCtrl', {
$scope: scope
});
}));
it('should have a HomeCtrl controller', function() {
expect(true).toBeDefined();
});
});
控制器代码:
files: [
'src/lib/angular/angular.min.js',
'src/lib/angular-mocks/angular-mocks.js',
'src/lib/angular-resource/angular-resource.min.js',
'src/lib/angular-route/angular-route.min.js',
'src/app/app.js',
'src/app/home/home.controller.js',
'src/test/specs/*.js'
],
我的karma.config.js中包含的文件 我添加了所有angularJS相关文件。 我还添加了我需要测试的控制器文件
fileLock is for using java.nio.channels.FileLock.
This option is not avail for the FTP component. This approach should be avoided when accessing a remote file system via a mount/share unless that file system supports distributed file locks.
善意地指出我,我做错了什么......
答案 0 :(得分:0)
在单元测试中模拟$ state对象。
var mockState = { go: function() {} };
var HomeCtrl = $controller('HomeCtrl', { $scope: scope, $state: mockState });
$ injector:modulerr很可能与您在控制器中使用$ state有关。您可以尝试将库添加到您的karma配置中,并在单元测试中加载模块,而不是模拟。
'src/lib/angular-ui-router/release/angular-ui-router.min.js'
beforeEach(module('ui.router'));