2013-08-09 5 views
52

GET/internalapi /引用符:は、なぜ私はエラーが表示される...予想外の要求を実行します。私は私の角度アプリで以下のサービスを定義した

services.factory('MyService', ['Restangular', function (Restangular) { 
     return { 
      events : { loading : true }, 

      retrieveQuotes : function() { 
       return Restangular.all('quotes').getList().then(function() { 
        return { hello: 'World' }; 
       }); 
      } 
    }; 
}]); 

と私はそれをテストするために、以下のスペックを書いています:

Error: Unexpected request: GET /internalapi/quotes

:私はテストを実行するたびに、最初のテストは、第二のテストに過ぎず合格

describe("MyService", function() { 

    beforeEach(module('MyApp')); 
    beforeEach(module("restangular")); 

    var $httpBackend, Restangular, ms; 

    beforeEach(inject(function (_$httpBackend_, _Restangular_, MyService) { 
     ms = MyService; 
     $httpBackend = _$httpBackend_; 
     Restangular = _Restangular_; 
    })); 


    it("retrieveQuotes should be defined", function() { 
     expect(ms.retrieveQuotes).toBeDefined(); 
    }); 

    it("retrieveQuotes should return array of quotes", function() { 

     $httpBackend.whenGET("internalapi/quotes").respond({ hello: 'World' }); 
     ms.retrieveQuotes(); 
     $httpBackend.flush(); 
    }); 

}); 

はエラーを生成します

私は間違って何をしていますか?

EDIT:

それは、私はそうのようなRestangular ... RestangularProvider.setBaseUrl("/internalapi");を設定したのだが判明しました。しかし、私はinternalapi/quotesへの呼び出しを偽っていました。 "/"がないことに注目してください。一度私はスラッシュを追加/internalapi/quotesすべては良かった:)

+1

のベストhttp://stackoverflow.com/questions/14761045/jasmine-tests-angularjs-directives-with-templateurl(私はので、重複としてフラグしませんでした問題はある程度異なって見える)。 –

+1

あなたの質問に解決済みのマークが付いていることを覚えておいてください。 :) –

+0

他の人には明らかかもしれませんが、そうでない場合、これらの 'expectMETHOD'コールは、外部サービスを呼び出す場合には、パスだけでなく完全なURLを渡すことを期待しています。 – kungphu

答えて

54

GET要求を期待するには、$ httpBackendに伝える必要があります。

describe("MyService", function() { 

    beforeEach(module('MyApp')); 
    beforeEach(module("restangular")); 

    var Restangular, ms; 

    beforeEach(inject(function (_Restangular_, MyService) { 
     ms = MyService; 

     Restangular = _Restangular_; 
    })); 


    it("retrieveQuotes should be defined", function() { 
     expect(ms.retrieveQuotes).toBeDefined(); 
    }); 

    it("retrieveQuotes should return array of quotes", inject(function ($httpBackend) { 

     $httpBackend.whenGET("internalapi/quotes").respond({ hello: 'World' }); 

     //expect a get request to "internalapi/quotes" 
     $httpBackend.expectGET("internalapi/quotes"); 

     ms.retrieveQuotes(); 
     $httpBackend.flush(); 
    })); 

}); 

また、あなたのexpectGET()であなたのrespond()を置くことができます。私はwhenGET()ステートメントをbeforeEach()に入れて、すべてのテストでレスポンスを定義する必要はありません。

 //expect a get request to "internalapi/quotes" 
     $httpBackend.expectGET("internalapi/quotes").respond({ hello: 'World' }); 

     ms.retrieveQuotes(); 
     $httpBackend.flush(); 
+3

残念ながら、 '' '$ httpBackend.expectGET(" internalapi/quotes ");' ''を追加しても、 、私は同じエラーを取得します。他のアイデア? –

+0

それはうまくいくはずです。ちょうど推測ですが、テストに '$ httpBackend'を直接注入してみてください。私はこれを示すために投稿を更新します。 –

+0

いいえ、同じこと。私は多くの例を見てきましたが、私はそれを正しく行っているようですが、残念なことに喜びはありません。 –

17

私はあなたと同じ問題を抱えていました。私の解決策は、.expectGETのURLパラメータの先頭に '/'を追加することでした。あなたの例を使用する:

$httpBackend.expectGET("/internalapi/quotes").respond({ hello: 'world'}) 

関連する問題