2017-12-18 9 views
1

컨트롤러에서 수행 된 서비스 호출을 테스트하는 코드를 작성하려고합니다. 나는 서비스 호출을하고 데이터를 가지고 컨트롤러에서 특정 기능을 단위 테스트하려고합니다. 현재 나는 지역 json과 함께 노력하고 있지만 실제로 서비스 호출을 할 것이다.서비스 호출을 위해 컨트롤러를 테스트 할 수 없습니다.

내가 먼저 스파이 개체를 만들었지 만 "TypeError : jasmine.CreateSpyObj가 함수가 아닙니다"라는 오류가 발생한다는 것을 알게되었습니다. 단위 테스트를 처음 사용했습니다. spyobject를 만들 수 없어 더 이상 진행할 수 없습니다. 코드를 저장하고 제발 나를 도와달라고 요청하십시오.

또한 정확하게 내가 spyObject를 만들면 정확히 무엇을해야할지 모르겠다. 실제로 서비스가 정상적으로 작동하는지 테스트하고 서비스 응답을 얻고 있는지 테스트하고 싶다.

제게 많은 시간을 할애하고 있습니다.

서비스 코드 :

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

unitTesting.json 코드 :

{ 
"name":"unit testing", 
"countryCode": "EG", 
"countryName": "Egypt", 
"region": "Africa" 
} 

컨트롤러 코드 :

getData: function(){ 
     appServices.getData().then(function(response) { 
      if (response && response.data) { 
       $scope.testVariable= response.data.name; 
      } 
     }); 
    }, 

단위 테스트 코드 :

describe('myCtrl', function() { 
     beforeEach(module('app')); 

     var $controller; 

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

describe('service call test', function() { 
var $http,$httpBackend,appServices, 
myService,$q,$rootScope,controller; 

var mockItem = 
     { 
     "name":"unit testing", 
     "countryCode": "EG", 
     "countryName": "Egypt", 
     "region": "Africa" 
     } 
beforeEach(inject(function(_$http_,_$httpBackend_,appServices,_$q_, 
_$rootScope_) { 
    $http = _$http_; 
    $httpBackend = _$httpBackend_; 
    appServices = appServices; 
    $rootScope = _$rootScope_; 
    $q =_$q_; 
    jasmine.spyOn(appServices, 'getData').and.returnValue($q.when(mockItem)); 
    controller = $controller('myCtrl', { $scope: $scope }); 
})); 
    it('Service call test ', function() { 
    controller = $controller('myCtrl', { $scope: $rootScope.new() }); 
    controller.getData(); 
    expect(appServices.getData).toHaveBeenCalled(); 
}); 
}); 
}); 

ERROR :

TypeError: jasmine.spyOn is not a function 
+0

소문자로 작성해야합니다.'jasmine.createSpyObj' –

+0

내 실수로 도움을 주셔서 감사합니다. 제 서비스 요청이 성공했는지 여부를 테스트하기위한 추가 단계를 알려주십시오. 나는 많은 솔루션을 온라인에서 보았고 혼란 스럽다. 추가 단계를 도울 수 있다면 좋을 것입니다. – GiggleGirl

+0

어떤 버전의 Jasmine을 사용하고 있습니까? –

답변

0

사용 spyOn 기능 : 당신은 다음처럼 테스트에서 전화를 확인할 수 있습니다

jasmine.spyOn(appServices, 'getData'); 

: 당신이 당신의 스펙을 작성한 방법을 찾고

expect(appServices.getData).toHaveBeenCalled(); 

, 실행할 수있는 다른 변경 사항을 제안 할 수 있습니까?

var $rootScope, 
    controller, 
    $q; 

beforeEach(inject(function(_$http_,_$httpBackend_,appServices,_$q_, _$rootScope_){ 
    $http = _$http_; 
    $httpBackend = _$httpBackend_; 
    appServices= appServices; 
    $rootScope = _$rootScope_; 
    $q = _$q_; 
    jasmine.spyOn(appServices, 'getData').and.returnValue($q.when(mockItem)); 
    controller = $controller('myCtrl', { $scope: $scope }); 
})); 
describe('when fetching data in the controller', function(){ 
    it('should delegate the call to appServices.getData()', function() { 
     controller = $controller('myCtrl', { $scope: $rootScope.new() }); 
     controller.getData(); 
     expect(appServices.getData).toHaveBeenCalled(); 
    }); 
}); 
+0

지금은 무엇입니까? – mindparse

+0

@Vitaliy Gorbenko, 내 단위 테스트 코드를 편집하여 그에 따라 적절하게 편집했지만 여전히 오류가 발생합니다. 내 코드를 원래의 질문을 오류 메시지와 함께 편집 해보십시오. – GiggleGirl

+0

별도로 연결할 수있는 방법이 있습니까? 나는 내 코드에서 중요한 것을 놓치고 있다고 느낀다. – GiggleGirl