2016-08-25 6 views
0

でサービスを呼び出すカルマテストコントローラ私のコントローラ関数getDataとファクトリ関数でテストを実行する最善の方法を教えてもらえますか?私は非常に混乱しており、どこから始めるべきか分かりません。以下のコードのテストをどのように記述しますか?http://

myApp.controller('myController', ['$scope', 'myFactory', function ($scope, myFactory) { 

    $scope.getData = function(id) { 
     var promise = myFactory.GetData('/dta/GetData?Id=' + id); 
     promise 
     .then(function (success) { 
      $scope.result = success; 
     }, function (error) { 
      $scope.error = true; 
     }); 
    } 
}); 


myApp.factory('myFactory', ['$http', function ($http) { 
    return { 
     GetData: function (url) { 
      return $http.get(url) 
      .then(function (response) { 
       return response.data; 
      }, function (error) { 
       return error; 
      }); 
     } 
    } 
}]); 
+0

はあなたがテストするために何をしたいかのラッパー、お使いのコントローラの 'getData'方法やあなたよりもあなたGetData工場がより便利にするために、さらに少し行くと思います工場の 'GetData'メソッド? – Phil

+0

...前者の場合、 'myFactory'の[模擬(スパイ)](http://jasmine.github.io/2.0/introduction.html#section-Spies:_ createSpyObj)を作成します。後者については、['$ httpBackend'](https://docs.angularjs.org/api/ngMock/service/$httpBackend)を使用してください。 – Phil

+0

上記のすべてのコードをテストしたいと思います。私は経験豊富なテスターが何をするのか分かりません。 – user1024941

答えて

1

各コンポーネントを個別にテストする必要があります(ユニットテストの対象)。したがって、このようなものは、コントローラあなたの工場のために

describe('myController test',() => { 
    let scope, myFactory; 

    beforeEach(() => { 
     myFactory = jasmine.createSpyObj('myFactory', ['GetData']);    

     module('your-module-name'); 
     inject(function($rootScope, $controller) { 
      scope = $rootScope.$new(); 

      $controller('myController', { 
       $scope: scope, 
       myFactory: myfactory 
      }); 
     }); 
    }); 

    it('getData assigns result on success', inject(function($q) { 
     let id = 1, success = 'success'; 
     myFactory.GetData.and.returnValue($q.when(success)); 

     scope.getData(id); 
     expect(myFactory.GetData).toHaveBeenCalledWith('/dta/GetData?Id=' + id); 
     scope.$digest(); // resolve promises 
     expect(scope.result).toBe(success); 
    })); 

    it('getData assigns error on rejections', inject(function($q) { 
     myFactory.GetData.and.returnValue($q.reject('error')); 

     scope.getData('whatever'); 
     scope.$digest(); 
     expect(scope.error).toEqual(true); 
    })); 
}); 

のために、あなたは別のdescribeを作成し、$httpBackendを注入して構成します。ドキュメントにはたくさんの例があります。


FYI、あなたはすなわち、あなたの工場で

return $http.get(url).then(response => response.data); 

をエラーハンドラを省略すべきかあなたが現在失敗した要求を変換しているとして、あなたはES2015

return $http.get(url).then(function(response) { 
    return response.data; 
}); 

を好きではない場合成功した約束に至る


実際に、私は単なる$http

GetData: function(id) { 
    return $http.get('/dta/GetData', { 
     params: { Id: id } 
    }).then(function(res) { 
     return res.data; 
    }); 
} 
+0

私は自分のファクトリをGetDataに変更しました:function(url){return $ http.get(url);}今は、line(scope.result).toBe(success);動作しません。私はこれが今何になるべきか分かりません。 – user1024941

+0

@ user1024941あなたは、約束事が解決された部分を 'response.data'で逃したようです。 – Phil

+0

サービスでthen節が必要なのはなぜですか?コントローラでアクセス権を取得してsuccess.dataにアクセスできるのはなぜですか? – user1024941

関連する問題