dimanche 21 décembre 2014

How can I test a angular services that use current $location to get the current lo get the current host using jasmine

I created a test with jasmine that check is the services that is giving me the endpoint of the API is working. I am getting the default value because the application in unit test in not running in localhost. So how can I mock that feature in mu unit test.


Here is my unit test:



describe("EndpointService->", getApiEndPoint);

function getApiEndPoint() {

beforeEach(function () {
module('app');
});

it("GetApiEndpointUriBaseOnCurrentHost", inject(function (endpointService) {
//Arrange
var expectedUriInLocalhostEnviroment = 'not recognized client host';

//Act
var uriEndPoint = endpointService.getApiEndpoint();
//Assert
expect(uriEndPoint).toMatch(expectedUriInLocalhostEnviroment);

}));
};


This is my service. It is using $location to get the local host:



(function () {
'use strict'
var app = angular.module('app');
app.factory('endpointService', endpointService);

function endpointService($location) {
return {
getApiEndpoint: function () {
var endpoint = '';
var host = $location.host();
switch ($location.host()) {
case 'localhost': endpoint = 'http://localhost:59987/'; break;
case 'projectDev': endpoint = 'http://project.com'; break;
default: endpoint = 'not recognized client host';
}
return endpoint;
}
}
};

})();

Aucun commentaire:

Enregistrer un commentaire