2015-11-23 2 views
5

私はカルマとジャスミンを使って私のAngularJSアプリケーションを単体テストしようとしています。私は$ httpサービスを模擬したい。そのために、私は$ httpBackendメソッドを使用しています。以下は、私がテストしたい私のサービスです。このサービスの

angular.module('MyModule').factory('MyService', function($http, $log, $parse, $q, $timeout, $filter, MyOtherService1, MyOtherService2){ 
var service = {}; 
    service.getSomething = function(id){ 
    return $http.get('/somePath/subpath/' + id); 
    } 
}); 

私のユニットテストは次のとおりです。

describe("myTest", function(){ 
    var myService, $httpBackend, scope, mockMyOtherService1, mockMyOtherService2; 

    var myResponse = 
    { 
     foo:'bar' 
    }; 

    beforeEach(module("MyModule")); 

    beforeEach(inject(function(_MyService_, $injector){ 

     $httpBackend = $injector.get('$httpBackend'); 
     myService = _MyService_; 
     scope = $injector.get('$rootScope').$new(); 
     mockMyOtherService1 = $injector.get('MyOtherService1'); 
     mockMyOtherService2 = $injector.get('MyOtherService2'); 

    })); 

    beforeEach(function(){ 
     //To bypass dependent requests 
     $httpBackend.whenGET(/\.html$/).respond(200,''); 
    }); 

    //If I uncomment the below afterEach block, the same error is shown at next line. 
    /*afterEach(function() { 
     $httpBackend.verifyNoOutstandingExpectation(); 
     $httpBackend.verifyNoOutstandingRequest(); 
    });*/ 

    //This test passes successfully 
    it("should check if service is instantiated", function() { 
     expect(myService).toBeDefined(); 
    }); 

    //This test passes successfully 
    it("should expect dependencies to be instantiated", function(){ 
     expect($httpBackend).toBeDefined(); 
    }); 

    //The problem is in this test 
    it("should get the getSomething with the provided ID", function() { 
     $httpBackend.whenGET('/somePath/subpath/my_123').respond(200,myResponse);    
     var deferredResponse = myService.getSomething('my_123'); 

     //The error is shown in next line. 
     $httpBackend.flush();  

     //If I comment the $httpBackend.flush(), in the next line, the $$state in deferredResponse shows that the Object that I responded with is not set i.e. it does not matches the 'myResponse'. 
     expect(deferredResponse).toEqual(myResponse); 

    }); 
}); 

これは緊急の問題であると私はできるだけ早く同じに関して助けが必要です。私はあなたの答えにとても感謝しています。

答えて

1

問題は、私は、彼らがサービスに注入されていない場合でも、私のスペックファイルに$の場所を注入するのに必要だった:だからにテストコードを変更します。注射後、すべてうまくいった!同じ状況で立ち往生している人を助けることを願っています。

+1

これは私にとってはうまくいかなかった:P –

+0

@TusharRajあなたは直面している正確な問題は何ですか?あなたの質問に私を指摘してくださいstackoverflow – Jagrut

1

あなたはあなたのサービスから約束を得るでしょう。

//The problem is in this test 
it("should get the getSomething with the provided ID", function (done) { 
    $httpBackend.expectGET('/somePath/subpath/my_123').respond(200,myResponse); 
    var deferredResponse = myService.getSomething('my_123'); 

    deferredResponse.then(function (value) { 
    expect(value.data).toEqual(myResponse); 
    }).finally(done); 

    $httpBackend.flush(); 
}); 
+0

私はあなたのコードを試しましたが、エラーは$ httpBackend.flush()行にあるので同じエラーが発生します。その行自体の後には実行されません。 $ httpBackend.flush()行をコメントアウトすると、それは動作します。しかし、$ httpBackend.flush()とafterEach()ブロックが必要です。 – Jagrut

+0

答えを更新し、flush()呼び出しを最後まで移動しました。今は自分のマシンで動作します。更新されたコードでもう一度お試しいただけますか? – ogugger

+0

まだ同じエラー!私は何かを見落としていると思うが、私はそれを見つけることができない。たとえ私が何も書いていないとしても、afterEach()は動作するはずですが、動作していません!だから私はそれをコメントして、さらに進めなければならなかった。しかし、それでも私は$ httpBackend.flush()で立ち往生しています! – Jagrut

1

私は最近、プロジェクトを角度1.2から1.4に更新するときにこの問題を抱えていました。テストコードは次のようになりました。

it('should call /something', function(){ 
    httpBackend.expectGET('/something').respond(200); 
    scope.doSomething(); 
    httpBackend.flush(); 
}); 

エラーは過去10回の繰り返しでした。これは.flush()メソッドを呼び出すことによって発生しました。私はdoSomething()の中に未定の約束がなかったので、これは一見あると分かりました。

doSomething()または内部の方法のどこかに約束を追加すると、infdigの問題は解決しました。

これは100%の推測ですので、開発に影響を与えないようにしてください。これは、httpBackendが約束を待つためにいくつかトリッキーを行うからです。変更があるまで繰り返し消化する必要があります。約束がないので、変更はありません。無限のダイジェストです。

関連する問題