我在我已经开发的Angularjs应用程序中尝试使用Jasmine和karma设置单元测试,如解释here。
我为我的控制器创建了一个简单的测试文件:
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.conf.js
// 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
})
}
但是在使用karma start
运行业力时,我遇到了以下错误:
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)
即使在包含ngMock依赖关系后仍未解决。这里出了什么问题?