我试图用Karma编写一些单元测试,并收到以下错误:
PhantomJS 1.9.8(Mac OS X)错误 错误:[$ injector:nomod] Module' portfolioMockupApp.services'不可用!您要么错误拼写了模块名称,要么忘记加载它。如果注册模块,请确保将依赖项指定为第二个参数。 http://errors.angularjs.org/1.3.3/ $注射器/ NOMOD?P0 = portfolioMockupApp.services at /Users/danielbogart/Documents/coding/work/portfolio-mockup/bower_components/angular/angular.js:1749
另外,我在portfolioMockupApp.services模块中有两个单独的文件,它们都保存在scripts / services目录中。
Karma.conf文件部分:
files: [
'bower_components/angular/angular.js',
'bower_components/angular-mocks/angular-mocks.js',
'bower_components/angular-animate/angular-animate.js',
'bower_components/angular-cookies/angular-cookies.js',
'bower_components/angular-resource/angular-resource.js',
'bower_components/angular-sanitize/angular-sanitize.js',
'bower_components/angular-touch/angular-touch.js',
'test/mock/**/*.js',
'test/spec/**/*.js',
'app/scripts/services/*.js',
'app/scripts/directives/*.js',
'app/scripts/controllers/*.js',
'app/scripts/app.js',
'node_modules/angular/angular.js',
'node_modules/angular-mocks/angular-mocks.js',
'./src/**/*.js',
'./test/**/*.js'
],
Portfolio.js规范(当前第一次也是唯一的测试):
'use strict';
describe('Controller: PortfolioCtrl', function () {
// load the controller's module
beforeEach(module('portfolioMockupApp', 'portfolioMockupApp.services'));
var PortfolioCtrl,
scope;
// Initialize the controller and a mock scope
beforeEach(inject(function ($controller, $scope, $log, $stateParams, $state,
$rootScope,portService, portfolioCreateService) {
scope = $rootScope.$new();
PortfolioCtrl = $controller('PortfolioCtrl', {
$scope: scope
});
}));
it('should have a list of 5 tabs by default', function () {
expect(scope.tabs.length).toBe(5);
});
});
答案 0 :(得分:14)
问题源于使用相同服务模块的两个单独的服务文件。在Karma.conf文件中,我必须显式加载初始化模块的服务文件,然后加载其他服务文件和其他应用程序。
'app/scripts/services/port-service.js',
'app/scripts/services/new-port-service.js',
'app/scripts/app.js',
'app/scripts/services/*.js',
'app/scripts/directives/*.js',
'app/scripts/controllers/*.js',
答案 1 :(得分:1)
感谢您重新使用解决方案。当两个模块相互依赖并存在于同一文件夹中时,我遇到了同样的问题,我们称之为app / scripts / parentModule.js和app / scripts / constants.js。两者都应该被karma.config.js中的通配符条目选中。
'app/scripts/*.js'
'app/scripts/anotherFolder/*.js'
由于constants.js依赖于parentModule.js,因此必须首先包含后者,我的猜测是通配符按字母顺序包含文件,但我还没有确认。
'app/scripts/parentModule.js'
'app/scripts/*.js'
'app/scripts/anotherFolder/*.js'