Ich versuche, einen Test für eine einfache Angular Factory schreiben, die eine $ http.get zurückgibt, aber mein Test schlägt fehl mit "Keine ausstehende Anfrage zu spülen!".
Ich habe ein paar der Beispiele, die ich online gefunden habe, gefolgt, aber nichts funktioniert.
Hier ist mein Service:
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
})
}
}}
)
Und hier ist meine spec:
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()
})
})
Was mache ich falsch?
der Tat macht es Sinn. Es behebt das Problem mit dem Test jedoch nicht: / –