Reputation: 2017
I have written a service which handles data with API. I have completed success response
unit testing for the same but facing issues with Unit Test of 404 Error Response
.
Please update answer with real scenarios as per question. I have already scene few tutorials but those are not helpful.
Here is my service code:
angular.module('myApp').service('SizingService',
function (SizerGatewayApi, RestDomains, $q, UnitySizerService) {
let api = new SizerGatewayApi(GatewayUrl);
return {
postSizingResults: postSizingResults
};
function postSizingResults(unitySizerDTO) {
let deferred = $q.defer();
function handleSuccessResponse(data) {
deferred.resolve(data);
UnitySizerService.resultSummary = data;
}
function handleErrorResponse(response) {
//Not able to cover UT for 404
if (response.status === 404) {
deferred.resolve(response.body);
} else {
deferred.reject(response);
}
}
api.post({dto: unitySizerDTO}).then(handleSuccessResponse, handleErrorResponse);
return deferred.promise;
}
}
);
Here is my spec file
describe('SizingService', function () {
beforeEach(module('basic-unity-replication-sizer-ui'));
let data = {
// response data
};
let SizerGatewayApiStub;
let unitySizerDTO = {
//post dto
};
let rootScope;
let $scope;
let SizingService;
let $q;
beforeEach(function () {
module(function ($provide) {
SizerGatewayApiStub = function () {
this.post = function (value) {
let deferred = $q.defer();
if (value.dto !== 'failure') {
deferred.resolve({data: data});
} else {
deferred.reject("no data");
}
return deferred.promise;
};
};
$provide.value('SizerGatewayApi', SizerGatewayApiStub);
});
inject(function (_$q_, $rootScope, _SizingService_) {
$q = _$q_;
rootScope = $rootScope;
$scope = $rootScope.$new();
SizingService = _SizingService_;
});
});
describe('should handle response', function () {
it('success', inject(function (SizingService) {
SizingService.postSizingResults(unitySizerDTO);
$scope.$apply();
expect(data).toBeDefined();
expect(data).not.toBeNull();
}));
//404 Failure scenarios needs to be covered by UT
it('failure', inject(function (SizingService) {
SizingService.postSizingResults(postWrongDTO);
$scope.$apply();
}));
});
});
Upvotes: 0
Views: 1579
Reputation: 2858
I'm actually having a difficult time understanding your code but what I "think" you will want to do is put a spy on SizerGateWayApiStub.post and return the rejected promise.
Something like this should get you on the correct path:
Add a variable let postDeferred;
Then in your beforeEach inject you will want to set up the spy
postDeferred = $q.defer();
spyOn(SizerGatewayApiStub, 'post').and.returnValue(postDeferred.promise);
Then in your test you would do something like this
it('failure', inject(function (SizingService) {
let mockResponse = {
status = "404",
body = "somevalue"
};
let result = SizingService.postSizingResults(postWrongDTO);
postDeferred.reject(mockResponse);
$scope.$apply();
expect(SizerGatewayApiStub.post).toHaveBeenCalled();
expect(result).toEqual(mockResponse.body);
}));
});
Upvotes: 1