can i set a base url for expect()

Issue

My restangular call has a baseUrl set in a config file to http://localhost:3000/. So a call like

Restangular.all("awards").customPOST(award)

Calls at baseUrl+"awards"

Now when I write a test for this, i have to write:

httpBackend.expectPOST("http://localhost:3000/awards")

But later if this baseUrl changes, I will have to change it in a lot many .expect() methods.

Is there anyway to set a baseUrl for the expect method, in a config file somewhere?

So that the expect method something like-

httpBackend.expectPOST(baseUrl + "awards");

So that any change in the baseUrl does not require any change in the expect() method?

Solution

You can create an angular.constant and then inject that constant wherever it is required.

var app = angular.module('app', []);
app.constant('Configuration', {
  BASE_URL: 'http://localhost:3000/'
});

app.factory('RestApiService', function($http, Configuration) {
  var awardApi = Configuration.BASE_URL + '/awards';

  return {
    getAwards: fucntion() {
      return $http.get(awardApi);
    };
  };
});

Answered By – Hitesh Kumar

Answer Checked By – Willingham (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.