I'm trying to unit test filter which has dependency on another service. I've read many answers regarding this problem but nothing solved mine. I use karma/Jasmine
the conf as follow
// 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: {
},
my application as follow
// 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
}]);
the test
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;
});
});
and I get the following error
error:[$injector:unpr] http://errors.angularjs.org/1.4.0-rc.2/$injector/unpr?=%24serviceNameProvider
Error: Declaration location at windows.inject.angular.mock.inject
first it is not needed to module( app.service),where the dependency service is located
otherwise, use provide function to provide the service when invoking the current module
var $filter, FilterNAMe ;
beforeEach(function () {
angular.mock.module("app.filter", function ($provide) {
$provide.value('serviceName');
});
});
then inject the filter
beforeEach( inject(function ( $filter) { //<-- Get the filter provider
FilterNAMe =$filter('FilterNAMe ',{});
}));
see the answer Injecting dependent services when unit testing AngularJS services