如何用Karma-Jasmine测试AngularJS控制器?依赖注入报错求助
AngularJS + Jasmine/Karma 单元测试:$injector:modulerr 错误解决
看起来你遇到的是AngularJS主库和angular-mocks版本不匹配导致的经典问题,这也是新手搭建测试环境时很容易踩的坑。
错误原因分析
从你给出的错误信息里能看到,你的AngularJS版本是1.5.0,但你的angular-mocks.js很可能不是同一个版本——$$asyncCallbackProvider这个内部服务在不同Angular版本里的存在状态或定义方式有变化,如果mocks版本和主库不对应,就会出现这种注入失败的错误。
解决步骤
严格匹配版本
找到你项目中使用的angular.js的版本(从错误日志看是1.5.0),然后下载完全相同版本的angular-mocks.js替换掉当前的文件。比如如果主库是angular-1.5.0.js,那mocks必须是angular-mocks-1.5.0.js,版本号要完全一致。确认加载顺序(你当前配置没问题,但再检查一遍)
在karma.conf.js的files数组里,确保angular.js是在angular-mocks.js之前加载的——你现在的顺序是对的:files: [ '/path/to/angular.js', '/path/to/angular-mocks.js', // 其他依赖和测试文件 ]这个顺序不能反,因为mocks依赖主Angular库的存在。
额外检查:排除列表是否合理
你排除了app.module.js,如果这个文件是你的根模块定义文件,那测试时是需要加载它的(因为你的测试里引用了ipaApp模块)。不过这不是当前错误的原因,但如果后续测试找不到ipaApp模块,记得调整这个排除规则。
你提供的代码片段(格式化后)
控制器测试代码
describe('controller test', function() { beforeEach(module('ipaApp')); var $controller, $rootScope; beforeEach(inject(function(_$controller_, _$rootScope_) { // The injector unwraps the underscores (_) from around the parameter // names when matching $controller = _$controller_; $rootScope = _$rootScope_; })); describe('$scope.grade', function() { it('sets the strength to "strong" if the password length is >8 chars', function() { var $scope = $rootScope.$new(); var controller = $controller('reportViewCardController', { $scope : $scope }); $scope.password = 'longerthaneightchars'; $scope.grade(); expect($scope.strength).toEqual('strong'); }); }); });
Karma配置文件
// Karma configuration // Generated on Sun Dec 31 2017 19:51:09 GMT+0530 (IST) module.exports = function(config) { config.set({ // base path that will be used to resolve all patterns (eg. files, exclude) basePath: '', // frameworks to use // available frameworks: https://npmjs.org/browse/keyword/karma-adapter frameworks: ['jasmine'], // list of files / patterns to load in the browser files: [ '/home/manoj/Desktop/workspace-development/dec-release/22-dec-17/bil-ipa/bil-ipa-ui/bil-ipa-ui-apm/src/main/webapp/resources/js/libs/angularjs/angular.js', '/home/manoj/Desktop/workspace-development/dec-release/22-dec-17/bil-ipa/bil-ipa-ui/bil-ipa-ui-apm/src/main/webapp/resources/js/libs/angularjs/angular-mocks.js', '/home/manoj/Desktop/workspace-development/dec-release/22-dec-17/bil-ipa/bil-ipa-ui/bil-ipa-ui-apm/src/main/webapp/resources/js/libs/angularjs/angular-route.js', '/home/manoj/Desktop/workspace-development/dec-release/22-dec-17/bil-ipa/bil-ipa-ui/bil-ipa-ui-apm/src/main/webapp/app/*', '/home/manoj/Desktop/workspace-development/dec-release/22-dec-17/bil-ipa/bil-ipa-ui/bil-ipa-ui-apm/src/main/webapp/test/*.js' ], // list of files / patterns to exclude exclude: [ '/home/manoj/Desktop/workspace-development/dec-release/22-dec-17/bil-ipa/bil-ipa-ui/bil-ipa-ui-apm/src/main/webapp/app/app.module.js', '/home/manoj/Desktop/workspace-development/dec-release/22-dec-17/bil-ipa/bil-ipa-ui/bil-ipa-ui-apm/src/main/webapp/resources/js/libs/angularjs/angular-cron-generator.min.js' ], // preprocess matching files before serving them to the browser // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor preprocessors: { }, // test results reporter to use // possible values: 'dots', 'progress' // available reporters: https://npmjs.org/browse/keyword/karma-reporter reporters: ['progress'], // web server port port: 9876, // enable / disable colors in the output (reporters and logs) colors: true, // level of logging // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG logLevel: config.LOG_INFO, // enable / disable watching file and executing tests whenever any file changes autoWatch: true, // start these browsers // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher browsers: ['Chrome'], // Continuous Integration mode // if true, Karma captures browsers, runs the tests and exits singleRun: false, // Concurrency level // how many browser should be started simultaneous concurrency: Infinity }) }
错误信息
Error: [$injector:modulerr] Failed to instantiate module ngMock due to: Error: [$injector:unpr] Unknown provider: $$asyncCallbackProvider http://errors.angularjs.org/1.5.0/$injector/unpr?p0=%24%24asyncCallbackProvider at /home/manoj/Desktop/workspace-development/dec-release/22-dec-17/bil-ipa/bil-ipa-ui/bil-ipa-ui-apm/src/main/webapp/resources/js/libs/angularjs/angular.js:68:12 at /home/manoj/Desktop/workspace-development/dec-release/22-dec-17/bil-ipa/bil-ipa-ui/bil-ipa-ui-apm/src/main/webapp/resources/js/libs/angularjs/angular.js:4397:19 at Object.getService [as get] (/home/manoj/Desktop/workspace-development/dec-release/22-dec-17/bil-ipa/bil-ipa-ui/bil-ipa-ui-apm/src/main/webapp/resources/js/libs/angularjs/angular.js:4550:39) at Object.decorator (/home/manoj/Desktop/workspace-development/dec-release/22-dec-17/bil-ipa/bil-ipa-ui/bil-ipa-ui-apm/src/main/webapp/resources/js/libs/angularjs/angular.js:4472:41) at /home/manoj/Desktop/workspace-development/dec-release/22-dec-17/bil-ipa/bil-ipa-ui/bil-ipa-ui-apm/src/main/webapp/resources/js/libs/angularjs/angular-mocks.js:1842:12 at Object.invoke (/home/manoj/Desktop/workspace-development/dec-release/22-dec-17/bil-ipa/bil-ipa-ui/bil-ipa-ui-apm/src/main/webapp/resources/js/libs/angularjs/angular.js:4604:19) at runInvokeQueue (/home/manoj/Desktop/workspace-development/dec-release/22-dec-17/bil-ipa/bil-ipa-ui/bil-ipa-ui-apm/src/main/webapp/resources/js/libs/angularjs/angular.js:4497:35) at /home/manoj/Desktop/workspace-development/dec-release/22-dec-17/bil-ipa/bil-ipa-ui/bil-ipa-ui-apm/src/main/webapp/resources/js/libs/angularjs/angular.js:4506:11 at forEach (/home/manoj/Desktop/workspace-development/dec-release/22-dec-17/bil-ipa/bil-ipa-ui/bil-ipa-ui-apm/src/main/webapp/resources/js/libs/angularjs/angular.js:321:20) at loadModules (/home/manoj/Desktop/workspace-development/dec-release/22-dec-17/bil-ipa/bil-ipa-ui/bil-ipa-ui-apm/src/main/webapp/resources/js/libs/angularjs/angular.js:4487:5)
内容的提问来源于stack exchange,提问作者Manoj Suthar
相关产品推荐
相关产品推荐

