Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the baseUrl (and other configuration settings) 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 the id field has been set correctly. Is this possible? I know I can view the route with the .route property on my service but that doesn't include the baseUrl.

like image 330
Adam Modlin Avatar asked Jul 17 '14 16:07

Adam Modlin


1 Answers

Late response I know, but I was wondering this myself and couldn't find a definite answer. The returned object has a 'configuration' property, which has 'baseUrl' and 'restangularFields' properties.

I'd also suggest moving the custom Restangular object into its 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 could test like so:

//sut is 'system under test', in your case the TestService

expect(sut.configuration.baseUrl).toEqual('api');
expect(sut.configuration.restangularFields.id).toEqual('id');
like image 128
pseudological Avatar answered Oct 16 '22 08:10

pseudological