컨트롤러에서 수행 된 서비스 호출을 테스트하는 코드를 작성하려고합니다. 나는 서비스 호출을하고 데이터를 가지고 컨트롤러에서 특정 기능을 단위 테스트하려고합니다. 현재 나는 지역 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
소문자로 작성해야합니다.'jasmine.createSpyObj' –
내 실수로 도움을 주셔서 감사합니다. 제 서비스 요청이 성공했는지 여부를 테스트하기위한 추가 단계를 알려주십시오. 나는 많은 솔루션을 온라인에서 보았고 혼란 스럽다. 추가 단계를 도울 수 있다면 좋을 것입니다. – GiggleGirl
어떤 버전의 Jasmine을 사용하고 있습니까? –