0
我三重檢查一切,還有似乎是一個錯誤
服務:
angular.module('app.services')
.factory('eventService', ['$http', '$q', function($http, $q) {
return {
get: function() {
var deffered = $q.defer();
$http.get('x')
.success(function(data) {
deffered.resolve(data);
})
.error(function(){
deffered.reject("could not access events");
});
return deffered.promise;
}
};
}])
單元測試:
describe('when fetching events by service', function() {
var service;
var $httpBackend;
var appConfig;
beforeEach(function() {
module("ngMockE2E");
module("app.services");
inject(function(eventService, _$httpBackend_) {
service = eventService;
$httpBackend = _$httpBackend_;
});
});
it('should get items', function(){
$httpBackend.expectGET('x').respond([{ name : 1}]);
var promise = service.get();
promise.then(function(data){
console.log(data);
});
$httpBackend.flush();
});
});
我正在以下錯誤的.flush()
Error: No pending request to flush !
at /test/lib/angular-mocks.js:1148
at /test/unit/services/EventServiceSpec.js:26
另外我如何確保$ httpBackend設置正被eventService使用。那eventService是由$ httpBackend的依賴而不是$ http生成的嗎?我認爲我可能是錯誤的原因。
沒有你它得到工作? –