My jasmine test for $httpBackend won't work unless I add the domain name, i.e. http://localhost:9808/
Since this test is used in several different environments I cannot use http://localhost:9808/
Without the domain I get this error:
Uncaught Error: Unexpected request: GET http://localhost:9808/api/mymethod No more request expected
describe('test my service', function () {
'use strict';
var $httpBackend,
myService;
beforeEach(inject(function (_$injector_, _$httpBackend_) {
var $injector = _$injector_;
$httpBackend = _$httpBackend_;
myService = $injector.get('myService');
// this works
//$httpBackend.whenGET('http://localhost:9808/api/mymethod').respond(200, mockData);
// this throws error
$httpBackend.whenGET('/api/mymethod').respond(200, mockData);
}));
afterEach(function () {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
it('check service exists and has methods', function () {
expect(myService).toBeDefined();
expect(myService.somemethod).toBeDefined();
$httpBackend.flush();
});
});
I even tried adding these lines to the beforeEach
// this also throws error
$httpBackend.when('GET', '/api/mymethod').respond(200, mockData);
// even adding expect still throws the error in the beforeEach $httpBackend.expectGET('/api/mymethod');
Any ideas or suggestions on how to get this to work without using the domain?
Here is the service code that actually makes the request:
(function(app) {
'use strict';
function MyService(apiService) {
var returnDataObject = [];
apiService.getAll().then(function(result) {
returnDataObject = result.data;
});
this.myMethod = function() {
if(returnDataObject) {
return returnDataObject;
}
else {
return null;
}
};
}
app.service('MyService', MyService);
MyService.$inject = ['APIService'];
})(angular.module('app.mymodule'));