我正在尝试对依赖于其他服务的单元测试过滤器。我已经阅读了很多关于这个问题的答案,但没有解决我的问题。 我用karma / Jasmine
conf如下
// Karma configuration
// Generated on Thu Nov 26 2015 07:47:24 GMT+0100 (Västeuropa, normaltid)
module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'
],
// list of files / patterns to load in the browser
files: [
'lib/angular.min.js',
'lib/angular-mocks.js',
'test.js',
'app/js/services/*.js',
'app/js/filters/*.js'
],
// list of files to exclude
exclude: [
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma- preprocessor
preprocessors: {
},
我的申请如下
// filter
var app=angular.module('app.filter',[]);
app.filter("filterName",['serviceName',function(ServiceName){
return function(input1,input2)
{
//some code
}]);
// service
var app=angular.module('app.service',[]);
app.service("serviceName", [$log,$windows,$route',function($log,$windows,$route){
//some functions
}]);
测试
describe('test filter with services dependencies',function()
{
beforeEach(module('app.service'));
beforeEach(module('app.filter'));
var FilterNAMe, ServiceNAMe ;
beforeEach(inject(function(_ServiceNAMe _,_FilterNAMe_) // i get error here
{
FilterNAMe = _FilterNAMe_;
ServiceNAMe= _ServiceNAMe_;
}));
it('',function()
{
//some test;
});
});
我收到以下错误
错误:[$注射器:unpr] http://errors.angularjs.org/1.4.0-rc.2/ $注射器/ unpr?=%24serviceNameProvider
错误:在windows.inject.angular.mock.inject声明位置
答案 0 :(得分:0)
首先,不需要依赖服务所在的模块(app.service)
否则,在调用当前模块时使用提供函数来提供服务
var $filter, FilterNAMe ;
beforeEach(function () {
angular.mock.module("app.filter", function ($provide) {
$provide.value('serviceName');
});
});
然后注入过滤器
beforeEach( inject(function ( $filter) { //<-- Get the filter provider
FilterNAMe =$filter('FilterNAMe ',{});
}));
看到答案 Injecting dependent services when unit testing AngularJS services