2

私は角度jを使用していますが、私はジャスミンテストケースに書きたいcontroller.jsを持っています。私はテストケース内でコントローラの機能を呼び出すことができます。私のコントローラには、スコープのメソッド(これは私がテストケースを書く)によって呼び出されるローカル関数があり、これはajax要求を持つサービスメソッドを呼び出しています。

私はあなたのために$ httpbackend

(function() { 
    'use strict'; 
    angular 
     .module('coApp') 
     .controller('NewController', [ 
     '$http', 
     '$q', 
     '$log', 
     '$modal', 
     '$scope', 
     'AgentDataLoader', 
     '$timeout', 
     NewController]); 

    function NewController ($http, $q, $log, $modal, $scope, $state, $rootScope, $filter, AgentDataLoader, $timeout) { 

     //Some variables declarations 

     //some methods 
     function myMethod($filter, a, b) { 
      console.log("inside myMethod"); 
      if (angular.isDefined(a) && angular.isDefined(b)) { 
       console.log("inside if "); 
       console.log("a:: "+a); 
       console.log("b:: "+b); 

       dataLoader.callPOST(a, b) 
        .then(function (data) { 
        console.log("Data from service :: "+JSON.stringify(data)); 
        /**calling some directive and methods 
         I am unable to execute this since I am unable to mock http Post of above mentioned dataLoader 
        **/ 

       console.log("after http request"); 
      } else { 
       //some other logic 
      } 
     } 



     $scope.methodToBeTested= function (randomData) { 
      console.log("selectedSystems called ****** "); 
      //some logic 
myMethod($filter, $scope.a, $scope.b); 

     }; 
})(); 

マイジャスミンテストスイート

describe('NewController',function(){ 
    var ctrl, scope, $httpBackend, $compile, parameters; 
    beforeAll(function (done) { 

    });  


    beforeEach(function() { 
     module('MYmODULE'); 
     module('myServiceModule'); 
    }); 

    beforeEach(inject(function ($controller, _$httpBackend_, $rootScope, _$compile_) { 
     scope=$rootScope.$new(); 
     $compile = _$compile_; 
     $httpBackend = _$httpBackend_; 
     ctrl=$controller('NewController',{$scope: scope}); 
     // Start listening to xhr requests 

     //This data i want to send as a success of httpBackEnd Post 
     success.data = JsonData; 

     console.log('Setting request in $httpBackend'); 

     //Not working 
     $httpBackend.whenPOST('/abc/efg').success(function(method, url, data) { 
      console.log("Getting sample data from httpBackend :: "+url); 
      return [200, success.data, {}]; 
     }); 

     //Not working 
     $httpBackend.whenPOST('abc/efg').respond(function(method, url, data, headers){ 
      console.log('Received these data:', method, url, data, headers); 
      phones.push(angular.fromJson(data)); 
      return [200, {}, {}]; 
     }); 

     $httpBackend.flush();  
    })); 

    it("Testing myMethodToBeTested of the page", function(){ 
     scope.randomData=someRandomData; 
     expect(scope.methodToBeTested(scope.randomData)); 

     //Tried this also not working 
     $httpBackend.expectPOST('/abc/efg').respond(success.data); 

     afterEach(function() { 
    }); 
}); 
+0

変更しますステータスコードを支援 –

答えて

0

あなたが応答データを模擬しなければならないテストケーススーツを使用して、私のテストケースでは、このHTTP POSTリクエストを模擬することはできませんよ。私は上記のコードで私は成功変数がどこでも初期化されていないと思う。だから、あなたが予想される応答を取得したい場合は、例のように(上記掲載のコードに基づいて)成功変数に期待されるオブジェクトを代入する必要があります。

var Success = {data:{SomeKey:"value"}} 

は今、あなたはPOST/GETリクエストでこの変数を渡す必要があり。同様

$httpBackend.expectPOST('/abc/efg').respond(Success.data); 

希望、それはポストコールであるとして、これは201に:)

関連する問題