Angular.js + Karma + Jasmine:未知的服务提供商
Angular.js + Karma + Jasmine: Unknown provider for service
我正在为以下 Angular.js 服务编写测试:
var module = angular.module('wp', [ 'aws', 'lodash', 'jquery', 'moment', 'wp.model' ]);
/**
* Wordpress service.
*/
module.service('wpService', function(_, $http, $q, $aws, Post) {
var self = this;
/**
* HTTP request.
*/
this.http = function(config) {
var $config = _.clone(config);
if ($config.user && $config.password) {
$config.headers = $config.headers || {};
$config.headers.Authorization = 'Basic ' + btoa($config.user + ':' + $config.password);
}
return $http($config);
};
// ..
}
测试用例如下:
/**
* Unit tests for wpService.
*/
describe('apService', function() {
var wpService;
beforeEach(angular.module('wp'));
beforeEach(inject(function(_wpService_) {
wpService = _wpService_;
}));
it('is defined', function() {
expect(wpService).toBeDefined();
});
});
这看起来就像教科书一样。不幸的是,我收到以下错误:
Chrome 43.0.2357 (Mac OS X 10.10.3) apService is defined FAILED
TypeError: queueableFn.fn.call is not a function
Error: [$injector:unpr] Unknown provider: wpServiceProvider <- wpService
http://errors.angularjs.org/1.4.1/$injector/unpr?p0=wpServiceProvider%20%3C-%20wpService
at /Users/jdolan/Coding/tuuli-syndicate/bower_components/angular/angular.js:68:12
我的 karma.config.js 包括模块以及 angular-mocks.js
:
// list of files / patterns to load in the browser
files : [ 'bower_components/jquery/dist/jquery.js',
'bower_components/lodash/lodash.js',
'bower_components/moment/moment.js',
'bower_components/x2js/xml2json.js',
'bower_components/aws-sdk/dist/aws-sdk.js',
'bower_components/angular/angular.js',
'bower_components/angular-route/angular-route.js',
'bower_components/angular-mocks/angular-mocks.js',
'app/**/*.js',
'tests/**/*.js' ],
我正在使用 Angular 1.4.1,Karma 0.12.36。
仔细阅读the angular-mocks example here。
angular.module()
函数 returns 一个实际的 angular 模块,而 module()
是 angular.mock.module()
的缩写。在您的代码中替换这一行,您应该已经准备就绪:
beforeEach(module('wp'));
我正在为以下 Angular.js 服务编写测试:
var module = angular.module('wp', [ 'aws', 'lodash', 'jquery', 'moment', 'wp.model' ]);
/**
* Wordpress service.
*/
module.service('wpService', function(_, $http, $q, $aws, Post) {
var self = this;
/**
* HTTP request.
*/
this.http = function(config) {
var $config = _.clone(config);
if ($config.user && $config.password) {
$config.headers = $config.headers || {};
$config.headers.Authorization = 'Basic ' + btoa($config.user + ':' + $config.password);
}
return $http($config);
};
// ..
}
测试用例如下:
/**
* Unit tests for wpService.
*/
describe('apService', function() {
var wpService;
beforeEach(angular.module('wp'));
beforeEach(inject(function(_wpService_) {
wpService = _wpService_;
}));
it('is defined', function() {
expect(wpService).toBeDefined();
});
});
这看起来就像教科书一样。不幸的是,我收到以下错误:
Chrome 43.0.2357 (Mac OS X 10.10.3) apService is defined FAILED
TypeError: queueableFn.fn.call is not a function
Error: [$injector:unpr] Unknown provider: wpServiceProvider <- wpService
http://errors.angularjs.org/1.4.1/$injector/unpr?p0=wpServiceProvider%20%3C-%20wpService
at /Users/jdolan/Coding/tuuli-syndicate/bower_components/angular/angular.js:68:12
我的 karma.config.js 包括模块以及 angular-mocks.js
:
// list of files / patterns to load in the browser
files : [ 'bower_components/jquery/dist/jquery.js',
'bower_components/lodash/lodash.js',
'bower_components/moment/moment.js',
'bower_components/x2js/xml2json.js',
'bower_components/aws-sdk/dist/aws-sdk.js',
'bower_components/angular/angular.js',
'bower_components/angular-route/angular-route.js',
'bower_components/angular-mocks/angular-mocks.js',
'app/**/*.js',
'tests/**/*.js' ],
我正在使用 Angular 1.4.1,Karma 0.12.36。
仔细阅读the angular-mocks example here。
angular.module()
函数 returns 一个实际的 angular 模块,而 module()
是 angular.mock.module()
的缩写。在您的代码中替换这一行,您应该已经准备就绪:
beforeEach(module('wp'));