0
재스민 테스트가 어떻게 작동하는지 이해하려고 시도합니다. 로컬 스토리지에서 JSON 문자열을 얻고 개체를 반환하는 서비스의 방법)재스민 컨트롤러 테스트의 각도 범위
var app = angular.module('planApp', []);
app.controller('PlanCtrl', function($scope, plansStorage){
var plans = $scope.plans = plansStorage.get();
$scope.formHidden = true;
$scope.togglePlanForm = function() {
this.formHidden = !this.formHidden;
};
$scope.newPlan = {title: '', description: ''} ;
$scope.$watch('plans', function() {
plansStorage.put(plans);
}, true);
$scope.addPlan = function() {
var newPlan = {
title: $scope.newPlan.title.trim(),
description: $scope.newPlan.description
};
if (!newPlan.title.length || !newPlan.description.length) {
return;
}
plans.push({
title: newPlan.title,
description: newPlan.description
});
$scope.newPlan = {title: '', description: ''};
$scope.formHidden = true;
};
});
plansStorage.get (이다 나는 모듈과 컨트롤러를 가지고있다.
var storedPlans = [
{
title: 'Good plan',
description: 'Do something right'
},
{
title: 'Bad plan',
description: 'Do something wrong'
}
];
describe('plan controller', function() {
var ctrl,
scope,
service;
beforeEach(angular.mock.module('planApp'));
beforeEach(angular.mock.inject(function($rootScope, $controller, plansStorage) {
scope = $rootScope.$new();
service = plansStorage;
spyOn(plansStorage, 'get').andReturn(storedPlans);
ctrl = $controller('PlanCtrl', {
$scope: scope,
plansStorage: service
});
spyOn(scope, 'addPlan')
}));
it('should get 2 stored plans', function(){
expect(scope.plans).toBeUndefined;
expect(service.get).toHaveBeenCalled();
expect(scope.plans).toEqual([
{
title: 'Good plan',
description: 'Do something right'
},
{
title: 'Bad plan',
description: 'Do something wrong'
}
]);
});
it('should add a plan', function() {
scope.newPlan = {title: 'new', description: 'plan'};
expect(scope.newPlan).toEqual({title: 'new', description: 'plan'});
scope.addPlan();
expect(scope.addPlan).toHaveBeenCalled();
expect(scope.plans.length).toEqual(3);
});
});
첫 번째 테스트가 확인 통과,하지만 두 번째 실패 :이 테스트를 실행
. scope.plans의 길이는 3이지만 2 일 것으로 예상됩니다. scope.plans는 scope.addPlan() 호출 후에 변경되지 않았습니다.
제가 이해한다면, addPlan 메소드의 $ scope는 두 번째 테스트에서 테스트하려는 범위와 다릅니다.
질문은 이유가 무엇입니까? 그리고 addPlan 메소드를 테스트하려면 어떻게해야합니까?