2

나는 각도 js를 사용하고 있으며 나는 재스민 테스트 케이스를 작성하고자하는 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