2017-03-08 11 views
0

$ HTTP.getを返す単純なAngularファクトリのテストを書こうとしていますが、 "保留中の要求がありません!

私はオンラインで見つかったいくつかの例に従ってきましたが、何も機能しません。

は、ここに私のサービスです:

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() 
    }) 
}) 

私が間違っているのは何?

答えて

0

工場内の約束を削除します。あなたはコントローラで約束どおりにアクセスしています。ちょうどこのようなHTTPリクエストを返します。この

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

のような、ここで

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

アクセスの約束は確かに、それは理にかなっています。ただし、テストの問題は修正されません。 –

関連する問題