2017-02-28 5 views
1

GeneControllerGeneというサービスを使用して、一連のAPI GETリクエストを作成しています。私はこのようなメインGETリクエストを嘲笑しましたし、私はそれが正常に動作していますかなり確信している:サービスリクエストにGETリクエストが掛からない

$httpBackend.expectGET '/api/knowledge/genes/param1' 
     .respond(200, JSON.stringify({ 
     data: { 
     mutationsUrl: 'http://localhost:3000/api/knowledge/genes/param1/mutations', 
     frequenciesUrl: 'http://localhost:3000/api/knowledge/genes/param1/frequencies', 
     annotationUrl: 'http://localhost:3000/api/knowledge/genes/param1/annotation', 
     sections: { 
     description: { 
      data: 'holder' 
      } 
     } 
     } 
     })) 

コントローラー: GeneController

.controller 'GeneController', Array '$scope', '$routeParams', '$http', 'Gene', ($scope, $routeParams, $http, Gene) -> 

    $scope.entity = Gene.get($routeParams 
     (entity) -> 
     $scope.description = entity.data.sections.description.data 

     entity.getUrl entity.data.mutationsUrl, {limit: 10}, (err, mutationsData) -> 
      if ! err? 
      for m in mutationsData.data 
       m.selected = true 
      $scope.mutations = mutationsData 

     entity.getUrl entity.data.frequenciesUrl, {}, (err, frequenciesData) -> 
      if ! err? 
      $scope.frequencies = frequenciesData 

     entity.getUrl entity.data.annotationUrl, {}, (err, annotationData) -> 
      if ! err? 
      $scope.annotation = annotationData 

     (error) -> 
     console.log error 
    ) 

サービス:ジーン

.factory 'Gene', Array '$resource', '$http', ($resource, $http) -> 
     Gene = $resource '/api/knowledge/genes/:gene', {}, 
     query: 
      method: 'GET' 

     Gene.prototype.getUrl = (url, options, callback) -> 
     $http {method: 'GET', url: url, params: options} 
      .then (res) -> # Success callback 
      callback null, res.data 
      ,(res) -> # Error callback 
      callback res.status, res.data 

     Gene 

私が抱えている問題は、Gene.prによって促進されている「セカンダリ」GETリクエストですototype.getUrlメソッド。私は適切な内容(突然変異、頻度、注釈から)がウェブページ上に表示されているので、メソッド自体は正常に動作していると思います。しかし、これらのGETリクエストは失敗しており、mochaから次のエラーが発生しています。「undefinedはオブジェクトではありません(mutationsData.dataを評価しています)」

私はこれらのGETリクエストに対する応答を無駄にしてしまいました。ここでコントローラのテストがあります。

テスト

... 

describe 'GeneController',() -> 

    Gene = undefined 

    beforeEach inject (_$controller_, $rootScope, _Gene_, _$httpBackend_) -> 
     scope = $rootScope.$new() 
     controller = _$controller_ 'GeneController', { 
     $scope: scope 
     $routeParams: { gene: 'param1' } 
     } 
     Gene: _Gene_ 
     $httpBackend = _$httpBackend_ 

    describe 'valid response',() -> 

     beforeEach() -> 
     $httpBackend.whenGET 'http://localhost:3000/api/knowledge/genes/param1/mutations' 
      .respond(200, JSON.stringify({ 
      data: "something" 
      })) 

     $httpBackend.whenGET 'http://localhost:3000/api/knowledge/genes/param1/frequencies' 
      .respond(200, JSON.stringify({ 
      data: 'somethingelse' 
     })) 

     $httpBackend.whenGET 'http://localhost:3000/api/kowledge/gene/param1/annotation' 
      .respond(200, JSON.stringify({ 
      data: 'somethingelser' 
     })) 

     $httpBackend.expectGET '/api/knowledge/genes/param1' 
      .respond(200, JSON.stringify({ 
      data: { 
      mutationsUrl: 'http://localhost:3000/api/knowledge/genes/param1/mutations', 
      frequenciesUrl: 'http://localhost:3000/api/knowledge/genes/param1/frequencies', 
      annotationUrl: 'http://localhost:3000/api/knowledge/genes/param1/annotation', 
      sections: { 
       description: { 
       data: 'holder' 
       } 
      } 
      } 
     })) 

     $httpBackend.flush() 

     it "should set $scope.description when the Gene is called",() -> 
     expect(scope.description).to.equal "holder" 

この問題を持つすべてのヘルプは非常に高く評価されるだろう。私はこれにかなり徹底的に取り組んでいます。事前にありがとう:)

答えて

0

コントローラはRest APIをRestfulな方法で呼び出していました。私は約束を実装するためにrestangularを使用して、各GETリクエストを独自のコードブロックに分割しました:

関連する問題