0
我想爲一個簡單的Angular工廠編寫一個測試,返回一個$ http.get,但是我的測試失敗並顯示「沒有待處理的請求刷新!」。
我跟着我在網上找到的一些例子,但沒有任何作品。
這裏是我的服務:
angular.module('MPMReportGenerator')
.factory('sectionService', function ($http, $log) {
return {
getSections: function() {
return $http
.get('/MPMReportGenerator/api/categories/all')
.then(function (response) {
return response.data
})
.catch(function (error) {
$log.error('ERROR:', error)
throw error
})
}
}}
)
這裏是我的規格:
describe('sectionService', function() {
'use strict'
var $httpBackend, sectionService, $rootScope
beforeEach(function() {
module('MPMReportGenerator')
inject(function ($injector) {
$httpBackend = $injector.get('$httpBackend')
$rootScope = $injector.get('$rootScope')
})
inject(function (_sectionService_) {
sectionService = _sectionService_
})
})
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation()
$httpBackend.verifyNoOutstandingRequest()
})
it('should have sectionService service be defined', function() {
expect(sectionService).toBeDefined()
})
it('should get the section list', function() {
var sections
$httpBackend.when('GET', '/MPMReportGenerator/api/categories/all').respond(200)
sectionService.getSections().then(function (data) {
sections = data
})
$rootScope.$digest()
expect($httpBackend.flush()).doesNotThrow()
})
})
我在做什麼錯?
事實上,這是有道理的。但它並沒有解決測試問題,但:/ –