2017-12-19 7 views
1

私のコントローラーで行われているサービス呼び出しをテストするためのコードを記述しようとしています。私はサービスコールをしているコントローラの特定の機能をユニットテストし、データを取得しようとしています。現在、私はローカルjsonで試していますが、実際にサービスコールを行います。コントローラーを介してテストHTTP呼び出しが発生しました

私が最初に私はスパイのオブジェクトを作成する必要がありますが、私はエラーを取得しています、私の目標が正常にユニットコントローラで起こっHTTP呼び出しをテストすることであることを知りました。

私はユニットテストの初心者です。私のコードを納得させてください、私に多くの時間を費やして苦労してください。また、私は多くの解決策を経てきました。感謝

サービスコード:

//xlpApp is the module name 
    xlpApp.factory('xlpServices', ['$rootScope', '$http', function($rootScope, 
    $http) { 
    var xlpServices = {}; 
    xlpServices.getProgramData = function(){ 
      return $http.get(scripts/services/data/unitTesting.json'); 
    }; 

unitTesting.jsonコード:

{ 
    "name":"unit testing" 
    } 

コントローラコード:

$scope.events = { 

    programData: function(){ 
      xlpServices.getProgramData().then(function(response) { 
       if (response && response.data) { 
        $scope.testVariable= response.data.name; 
       } 
      }); 
     }, 
    selectSortingType : function(type) { 
      $scope.selectedSorting = type; 
      selectedFilter.sortingBy = $scope.selectedSorting; 
     } 

} 
$scope.events.programData(); 

ユニットテストコード:

describe('myProgramGpmCtrl', function() { 
    beforeEach(module('xlp')); 

    var $controller; 

    beforeEach(inject(function(_$controller_){ 
    $controller = _$controller_; 
    })); 

describe('service call test', function() { 
    var xlpServices , myService , $q; 
    var $scope = {}; 

    beforeEach(inject(function(xlpServices,_$q_){ 
     xlpServices = xlpServices; 
     $q = _$q_; 
     var controller = $controller('myProgramGpmCtrl', { $scope: $scope }); 
     myService = jasmine.createSpyObj('xlpServices',['getProgramData']); 
    })); 

    it('Service call test ', function() { 
     expect(myService.getProgramData).toHaveBeenCalled(); 
    }); 
}); 

}); 

ERROR:

 Expected spy xlpServices.getProgramData to have been called. 

答えて

1

試してみてください何かのように、

describe('service call test', function() { 
    var xlpServicesMock , myService , $q; 
    var $scope = {}; 

    beforeEach(inject(function(xlpServices,_$q_){ 
     xlpServicesMock = xlpServices; 
     $q = _$q_; 
     spyOn(xlpServicesMock ,'getProgramData').and.callFake(function() { 
     // we can return promise instead this custom object 
     return { 
      then: (callback, errorCallback) => { 
       callback('data to be passed to then callback'); 
       /* `callback` function should be invoked when you wanted to test the service for success response with status code 200. 
        `errorCallback` function should be invoked with 500 status code when you wanted to test the api failure 
       Ex: callback({status: 200, data: <your data here>); 
        errorCallback({status: 500, data: <your error data>}) 
       You can prepare the response to be passed as you wanted. 

       */ 

      } 
     }; 
     }); 

     var controller = $controller('myProgramGpmCtrl', { $scope: $scope, xlpServices: xlpServicesMock }); 

    })); 

    it('Service call test ', function() { 
     $scope.events.programData(); 
     expect(myService.getProgramData).toHaveBeenCalled(); 
    }); 
}); 

がオンラインで利用可能な優れたリソースがあります。 チェックherehere

+0

おかげであなたの助けのためにたくさん。わたしにはできる 。 toHaveBeenCalled()関数を使用した後に、サービス応答が200であるかどうかを確認する方法を教えてください。応答が500の場合、どうすれば知ることができますか。 – GiggleGirl

+1

答えを更新しました – Thaadikkaaran

+0

ご協力ありがとうございます。 – GiggleGirl

関連する問題