2016-08-25 1 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'의 [mock (spy)] (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을 구성합니다. 문서에는 많은 예제가 있습니다.


참고로, 당신은 즉, 귀하의 공장에서

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

내 컨트롤러에서 success.data에 액세스 할 수있을 때 서비스에서 then 절이 필요한 이유는 무엇입니까? – user1024941