我爲$茉莉測試httpBackend由於本次測試在幾個不同的環境下使用,我不能使用http://localhost:9808/
不會工作,除非我添加的域名,即 http://localhost:9808/沒有域我得到這個錯誤:
未捕獲的錯誤:意外的請求:GET http://localhost:9808/api/mymethod 沒有更多的要求預計
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();
});
});
我甚至嘗試添加這些行到beforeEach
//這也將引發錯誤
$ httpBackend.when( 'GET',「/api/mymethod').respond(200,mockData) ;
//甚至加上expect仍會拋出beforeEach的錯誤 $ httpBackend.expectGET('/ api/mymethod');
有關如何在不使用域的情況下使其工作的任何想法或建議?
這裏的服務代碼,實際上發出請求:
(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'));
實際發出請求的代碼是什麼? –