Jasmine / AngularJS: Depends on service in service in unit test?

I have angular modules:

var app = angular.module("SearchUI",[]); 

I have a configService service that supports a set of configuration parameters:

 app.provider("configService",function(){ //stuff here }) 

I conducted jasmine block tests in configService fineL

 describe('configService',function(){ var configService,$httpBackend; beforeEach(module('SearchUI')); beforeEach(inject(function(_configService_,$injector){ configService = _configService_; $httpBackend = $injector.get("$httpBackend"); })); it('should have default values for configService', function(){ expect(configService.getDefaultSearch()).toEqual(['abstract','title','keyword','keywordplus' ]); }); //other stuff 

all tests pass normally.

however, I don't understand how to maintain this injection in another service:

In my application:

 app.service("SearchService",function($http,$log,configService,$q){ //stuff search_params = configService.getDefaultSearch(); }) 

my specification:

 describe('SearchService',function(){ var searchService,configService; beforeEach(module('SearchUI')); beforeEach(inject(function(_configService_,_SearchService_){ configService = _configService_; searchService = _SearchService_; })); it('SearchService should return results', function(){ var waiting = searchService.SimpleSearch("card","wos",0); //other stuff 

the specification does not work because the following is required in the simplesearch function:

 search_params = configService.getDefaultSearch(); //get the default search parameters 

My question is: how do I insert the required service into the ANOTHER service?

+6
source share
2 answers

Services are just JavaScript classes, and you can instantiate them without using the angular input mechanism to facilitate your dependency injection. Instead, you can simply create a new instance of the class yourself, specifying the necessary parameters.

You are currently creating your service using the built-in function:

app.service ("SearchService", function ($ HTTP, $ lag, ConfigService, $ q) {...

Instead, by making a small tweak that will separate the announcement of the service from its injection into the angular module. This will allow you to access the service class from your test.

 function SearchService($http, configService){... app.service("SearchService", SearchService); 

From your test, you can prepare your injection drugs in your preprocessor before the following:

 describe('configService',function(){ var configService, httpBackend; beforeEach(module('SearchUI')); beforeEach(inject(function($httpBackend){ httpBackend = "$httpBackend"; configService = jasmine.createSpyObj('configService', ['getDefaultSearch']); })); /* helper method that I create to only have one place where the service is created If I add a new param/dependency then I only have to change the construction once */ function createService(){ return new SearchService(httpBackend, configService); } }); 

The main reason for applying this approach to testing (controlling dependency injection manually rather than by implementing angular) is to have full control and really isolate the element I'm trying to test.

+3
source

configService and SearchService are init in the module application, but not in the SearchUI module. Replace "beforeEach (module (" SearchUI ")); by" beforeEach (module ('myapp')); "

+1
source

Source: https://habr.com/ru/post/985395/


All Articles