My current task involves testing a service that utilizes $http
var APIClient = function($http) {
this.send = function(data) {
$http({
method: data.method,
url: data.url,
headers: data.headers,
data: data.data
}).success(function(response, status) {
data.success(response, status);
}).error(function(response, status) {
data.error(response, status);
});
}
}
angular.module('api.client', []).factory('APIClient', ['$http'
function($http) {
var client = new APIClient($http);
return {
send: function(data) {
return client.send(data);
},
}
}
]);
As part of the testing process, I have constructed the following test scenario:
describe('send', function() {
var apiClient, $httpBackend;
beforeEach(module('compare'));
beforeEach(inject(function($injector) {
$httpBackend = $injector.get('$httpBackend');
apiClient = $injector.get('APIClient');
}));
it('The existence of send() function should be verified', function() {
expect(apiClient.send).toBeDefined();
});
it('A GET request should be sent successfully', function(done) {
var url = '/';
$httpBackend.expect('GET', url).respond({});
apiClient.send({
url: url,
success: function(data, status) {
console.log(status);
done();
},
error: function(data, status) {
console.log(status);
done();
}
});
$httpBackend.flush();
});
});
However, during testing, an unexpected error keeps occurring:
PhantomJS 1.9.8 (Mac OS X) send Should send GET request FAILED
Error: Unexpected request: GET templates/test.html
Expected GET /
The issue seems to stem from the fact that the URL being tested keeps changing to 'templates/test.html' instead of staying at '/' which was provided in my code snippet in app.js.