There is a service 'ApiService' containing a function 'get' :-
app.factory('ApiService', function($http) {
var get = function(url) {
if (url.indexOf('?') === -1) {
url = url + '?debug=true';
} else {
url = url + '&debug=true';
}
return $http({
method: 'GET',
url: url
}).then(function(response) {
return response;
})
}
This is unit test code:
describe('Service: ApiService', function() {
var apiServiceObj, mockHttpBackend;
// load the service's module
beforeEach(module('icrmappApp'));
beforeEach(function() {
module(function($provide) {
$provide.service('ApiService', function() {
this.get = jasmine.createSpy('get');
});
});
});
beforeEach(inject(function($httpBackend, ApiService) {
apiServiceObj = ApiService;
mockHttpBackend = $httpBackend;
mockHttpBackend.when('GET', 'icrm/v1/agents/status')
.respond([
{"id": 1},
{"agentId": 1254167},
{"status": 0},
{"lastActivityTime": 1436428713000},
{"activityTimeInSecs": 145}
])
}));
it('.get must return correct mock data', function() {
/*
var result = apiServiceObj.get('icrm/v1/agents/status');
expect(result).toBe([
{"id": 1},
{"agentId": 1254167},
{"status": 0},
{"lastActivityTime": 1436428713000},
{"activityTimeInSecs": 145}
]);
*/
mockHttpBackend.expectGET('icrm/v1/agents/status').respond(function(){
return
[
{"id": 1},
{"agentId": 1254167},
{"status": 0},
{"lastActivityTime": 1436428713000},
{"activityTimeInSecs": 145}
]
});
});
});
Mocking $httpBackend works properly as expected. (Code not in comments of the unit test).
But when called via another service http mocking isn't working(Code in the comments of the unit test case).
I'm only 3 days experienced to unit testing and have done quite a searching.
Kindly guide me through the conceptual errors.
Aucun commentaire:
Enregistrer un commentaire