2013-11-04 5 views
18

ユーザーがボタンをクリックしたときにサービスの状態を取得する機能を持っていますか、または何らかのイベントが発生してこの機能が自動的に呼び出されたときに 。私はまた、他の$ httpBackendのテストを持っているユニットテストで

describe('SendServiceCtrl', function(){ 
    var scope, ctrl, $httpBackend, configuration; 

    beforeEach(function() { 
     module('papi', 'ngUpload'); 
    }); 

    beforeEach(inject(function(_$httpBackend_, $rootScope, $controller, config) { 
     configuration = config; 
     $httpBackend = _$httpBackend_; 
     $httpBackend.expectGET(configuration.entrypoint + configuration.api + "/user/outboundstatus/").respond(200, {"meta":{"apiVersion":"0.1","code":200,"errors":null},"response":{"allowed":false}}); 
     scope = $rootScope.$new(); 
     ctrl = $controller('SendServiceCtrl', {$scope: scope}); 
    })); 

    it('Should get the status', function() { 

    scope.serviceId = '09bb5943fa2881e1'; 
    scope.getStatus(); 
    $httpBackend.whenGET(configuration.entrypoint + configuration.api + '/outbound/service/' + scope.serviceId).respond(200, {"meta":{"apiVersion":"0.1","code":200,"errors":null}}); 

    }); 

}); 

:ユニットテストは次のように作られて

$scope.getStatus = function() { 
    $http({method: 'GET', url: config.entrypoint + config.api + '/outbound/service/' + $scope.serviceId}) 
    .success(function(data) { 
     $scope.errorMessage = ''; 
     $scope.serviceAllGood = data; 
    }) 
    .error(function() { 
     $scope.serviceAllGood = ''; 
     $scope.errorMessage = 'We are experiencing problems retrieving your service status.'; 
    }); 
    } 

これは私が使用していますコントローラで定義された私の関数であります同じコントローラですが、それらのすべてがちょうどsomoothelyで動作します。私は間違って何をしていますか?

答えて

12

whenGETの前に入力する必要があります。

it('Should get the status', function() { 
    scope.serviceId = '09bb5943fa2881e1'; 
    $httpBackend.whenGET(configuration.entrypoint + configuration.api + '/outbound/service/' + scope.serviceId).respond(200, {"meta":{"apiVersion":"0.1","code":200,"errors":null}}); 
    scope.getStatus(); 
}); 

要求の期待値を設定し、要求をトリガーします。

これが役に立ちます。

+0

テンプレートでもこのエラーが発生します。 – FlavorScape

+0

'$ templateCache'を' ui.bootstrap'のように使うと、GETを嘲笑する必要が無くなると思いますか? – domokun

+0

@domokunあなたはそれが使われている場所へのリンクを投稿できますか? –

関連する問題