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

改變你的狀態碼201,因爲它是後調用 –

回答

0

嘲笑在我的測試情況下,該HTTP POST請求你測試用例適合你有嘲笑的響應數據。我認爲在上面的代碼中,我沒有看到成功變量在任何地方都處於初始狀態。所以,如果你想獲得預期的反應,你必須分配在成功變量的預期目標(基於上面貼的代碼),如例:

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

現在你必須通過這個變量的POST/GET請求。像

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

希望這有助於:)