2016-06-14 2 views
0

に呼び出し、ちょっと私ははジャスミン

  1. どのように私は私の残りの呼び出しに責任があるAngularJSのサービスをテストする必要があり、私が把握しようとしています、AngularJSテストに新しいです。

  2. テストしたい他のコントローラでこのサービスをどのように呼び出すのですか?私は残りの部分をテストする必要が コードを要求使いのDataFactoryサービスをテストする必要が

は、このようなものです:

var app = angular.module("app",[]); 
 

 
app.controller("mainCTRL", ["$scope","dataFactory",function($scope,dataFactory){ 
 
    $scope.title = "Hello World"; 
 
    dataFactory.getEntries("fakeSuffix"); 
 
    
 
    }]); 
 

 
app.factory('dataFactory', ['$http', '$window', '$log', function ($http, $window, $log) { 
 
      var urlBase = $window.location.origin + '/api', 
 
       dataFactory = {}; 
 
      /** 
 
      * get all Entries. 
 
      **/ 
 
      dataFactory.getEntries = function (suffix) { 
 
       $log.debug("************ Get All Entries ************"); 
 
       $log.debug("url:", urlBase + suffix); 
 
       return $http.get(urlBase + suffix, { headers: { cache: false } }); 
 
      }; 
 

 
      /** 
 
      * get single Entry. 
 
      **/ 
 
      dataFactory.getEntry = function (id) { 
 
       $log.debug("************ Get Single Entry ************"); 
 
       return $http.get(urlBase + '/' + id); 
 
      }; 
 

 
      /** 
 
      * insert Entry 
 
      **/ 
 
      dataFactory.postEntry = function (method, entry) { 
 
       var url = urlBase + '/' + method; 
 
       return $http.post(url, entry); 
 

 
      }; 
 

 
      /** 
 
      * update Entry 
 
      **/ 
 
      dataFactory.updateEntry = function (entry) { 
 
       $log.debug("************ Update Single Entry ************"); 
 
       return $http.put(urlBase + '/' + entry.id, entry); 
 
      }; 
 

 
      /** 
 
      * delete Entry 
 
      **/ 
 
      dataFactory.deleteEntry = function (id) { 
 
       $log.debug("************ Delete Single Entry ************"); 
 
       return $http.delete(urlBase + '/' + id); 
 
      }; 
 

 
      return dataFactory; 
 
     }]);
<script src="https://ajax.googleapis.com/ajax/libs/jQuery/2.1.1/jquery.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 

 
<div id="block" ng-app="app" ng-controller="mainCTRL"> 
 
{{title}} 
 
</div>

答えて

1

Q.1 - 私の休息を担当しているAngularJSサービスをテストするにはどうすればよいですか?
Ans。 - 下記のいずれかのサービスメソッドのテストケースを作成しました。他のメソッドにも同様のテストケースを書くことができます。

Q.2 - テストしたい他のコントローラでこのサービスをどのように呼び出すのですか?
Ans。は - あなたが$httpgetpostputdelete方法に頼っているあなたのサービスのためのテストケースを書いているよう以下のコードでは、私がテストケースで$httpBackendを注入し、beforeEachブロック内のすべてのこれらのメソッドを嘲笑しました。同様に、このサービスに依存するコントローラ用のテストケースを作成する場合は、同様の方法を使用する必要があります。コントローラのテストケースにサービスを注入し、コントローラから呼び出されるサービスのすべてのメソッドをモックしてください。

//AngularJS Code 

var app = angular.module("app",[]); 

app.controller("mainCTRL", ["$scope","dataFactory",function($scope,dataFactory){ 
    $scope.title = "Hello World"; 
    dataFactory.getEntries("fakeSuffix"); 
}]); 

app.factory('dataFactory', ['$http', '$window', '$log', function ($http, $window, $log) { 
    //var urlBase = $window.location.origin + '/api', 
    var urlBase = '/api', //Change here. 
    dataFactory = {}; 
    /** 
    * get all Entries. 
    **/ 
    dataFactory.getEntries = function (suffix) { 
     $log.debug("************ Get All Entries ************"); 
     $log.debug("url:", urlBase + suffix); 
     return $http.get(urlBase + suffix, { headers: { cache: false } }); 
    }; 

    /** 
    * get single Entry. 
    **/ 
    dataFactory.getEntry = function (id) { 
     $log.debug("************ Get Single Entry ************"); 
     return $http.get(urlBase + '/' + id); 
    }; 

    /** 
    * insert Entry 
    **/ 
    dataFactory.postEntry = function (method, entry) { 
     var url = urlBase + '/' + method; 
     return $http.post(url, entry); 
    }; 

    /** 
    * update Entry 
    **/ 
    dataFactory.updateEntry = function (entry) { 
     $log.debug("************ Update Single Entry ************"); 
     return $http.put(urlBase + '/' + entry.id, entry); 
    }; 

    /** 
    * delete Entry 
    **/ 
    dataFactory.deleteEntry = function (id) { 
     $log.debug("************ Delete Single Entry ************"); 
     return $http.delete(urlBase + '/' + id); 
    }; 

    return dataFactory; 
}]); 

//Jasmine Test Case 
describe('factory: dataFactory', function() { 

    var dataFactory, $http, $window, $log, $httpBackend; 

    beforeEach(module('app')); 

    beforeEach(inject(function (_dataFactory_, _$http_, _$window_, _$log_, _$httpBackend_) { 
     dataFactory = _dataFactory_; 
     $http = _$http_; 
     $window = _$window_; 
     $log = _$log_; 
     $httpBackend = _$httpBackend_; 

     $httpBackend.when('GET', "/api/suffix").respond({ 
      status: 200, 
      data: "data" 
     }); 

     $httpBackend.when('GET', "/api/id").respond({ 
      status: 200, 
      data: "data" 
     }); 

     $httpBackend.when('POST', "/api/method").respond({ 
      status: 200, 
      data: "data" 
     }); 

     $httpBackend.when('PUT', "/api/id").respond({ 
      status: 200, 
      data: "data" 
     }); 

     $httpBackend.when('DELETE', "/api/id").respond({ 
      status: 200, 
      data: "data" 
     }); 
    })); 

    afterEach(function() { 
     $httpBackend.verifyNoOutstandingExpectation(); 
     $httpBackend.verifyNoOutstandingRequest(); 
    }); 

    describe('function: getEntries', function(){ 
     //A sample test case for getEntries 
     it('should get all enteries', function(){ 
      var response = dataFactory.getEntries("/suffix"); 
      response.then(function(res){ 
       expect(res.status).toEqual(200); 
      }); 
      $httpBackend.flush(); 
     }); 
    }); 

    //Similarly write tests for the rest of functions. 

}); 

これが役に立ちます。

1

への答えで見つけ、これを試してみてくださいこの質問Injecting a mock into an AngularJS service

module(function($provide) { 
     $provide.value('$http', { 
      get: function(url, options) { 
       // your get implementation 
      }, 
      post: function(url, data) { 
       // your post implementation 
      }, 
      'delete': function(url) { 
       // your delete implementation 
      }, 
      put: function(url, data) { 
       // your put implementation 
      } 
     }); 
    }); 
1

私が知っている限り、サービスをテストするには、コントローラーの初期化を行わずにコントローラーと同様のジャスミンテストケースを作成します。

サービス応答に基づいてコントローラをテストするには、それぞれのサービスに対してspyOnを作成し、サービス応答をモックします。