How can I get baseUrl (and other configuration options) from a Restangular object?

Say I define a factory as follows:

angular.module('myServices').factory('TestService', ['Restangular', function(restangular){ return restangular.withConfig(function(RestangularConfigurer) { RestangularConfigurer.setBaseUrl('api'); RestangularConfigurer.setRestangularFields({ id: 'Id'}); }; }).all('Job'); }]); 

I would like to be able to write unit tests to confirm that my baseUrl and id fields baseUrl set correctly. Is it possible? I know that I can view the route using the .route property on my service, but this does not include baseUrl .

+7
angularjs restangular
source share
1 answer

The late answer, which I know, but I asked myself this and could not find a definite answer. The returned object has the configuration property, which has the properties baseUrl and restangularFields.

I would also suggest moving the custom Restangular object to my own service:

 angular.module('myServices') .factory('TestService', ['Restangular', function(restangular) { return restangular.withConfig(function(RestangularConfigurer) { RestangularConfigurer.setBaseUrl('api'); RestangularConfigurer.setRestangularFields({ id: 'Id'}); }); } ]) .factory('Job', ['TestService', function (TestService) { return TestService.all('Job'); } ]); 

So you can test like this:

 //sut is 'system under test', in your case the TestService expect(sut.configuration.baseUrl).toEqual('api'); expect(sut.configuration.restangularFields.id).toEqual('id'); 
+1
source share

All Articles