Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 测试$http Angular服务:没有要刷新的挂起请求_Javascript_Angularjs_Unit Testing_Karma Jasmine - Fatal编程技术网

Javascript 测试$http Angular服务:没有要刷新的挂起请求

Javascript 测试$http Angular服务:没有要刷新的挂起请求,javascript,angularjs,unit-testing,karma-jasmine,Javascript,Angularjs,Unit Testing,Karma Jasmine,我试图为一个简单的Angular工厂编写一个测试,该工厂返回一个$http.get,但是我的测试失败了,因为“没有待处理的刷新请求!” 我在网上找到了一些例子,但都不管用 这是我的服务: angular.module('MPMReportGenerator') .factory('sectionService', function ($http, $log) { return { getSections: function () { return $htt

我试图为一个简单的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()
  })
})

我做错了什么?

在工厂内取消承诺。您正在访问控制器中的承诺。只需像这样返回http请求

angular.module('MPMReportGenerator')
    .factory('sectionService', function($http, $log) {
        return {
            getSections: function() {
                return $http
                    .get('/MPMReportGenerator/api/categories/all')
            }
        }
    })
像这样进入这里

 sectionService.getSections().then(function(response) {
    sections = response.data // the values comes under data property so need to access like this.
 })

事实上,这是有道理的。但是,它无法解决测试中的问题:/