ngClick 지시문에 바인딩 된 함수를 단위 테스트하려고합니다. 우리가이 프로젝트를 시작했습니다 그것은 지금은 이런 걸보고 내가 멀리에 도착하기 전에 나는 몇 가지 테스트 적용하려면이 같은
vm.open = function($event) {
$event.preventDefault();
$event.stopPropagation();
vm.opened = true;
};
I 단위 테스트 :
describe('Unit: simpleSearchController', function(){
//include main module
beforeEach(module('myApp'));
var ctrl, scope, event ;
// inject the $controller and $rootScope services
// in the beforeEach block
beforeEach(inject(function($controller, $rootScope){
// Create a new scope that's a child of the $rootScope
scope = $rootScope.$new();
// Create the controller and alias access using controllerAs
ctrl = $controller('simpleSearchController as vm', {
$scope: scope
});
}));
// unit tests
it('should set vm.opened to true', function(){
event = scope.$broadcast("click");
expect(event).toBeDefined();
scope.vm.open(event);
expect(event.defaultPrevented).toBeTruthy();
expect(scope.vm.opened).toBeTruthy();
});
});
카르마를 이 오류가 발생하는 테스트를 실행합니다.
TypeError: $event.stopPropagation is not a function.
아이디어가 있으십니까?
'$ ($ event) .stopPropagation();'시도 했습니까? – algorhythm
이벤트를 브로드 캐스트 할 때 전파 전파가 제대로되지 않습니까? 방송이 중단되면 stopPropagation이 방출됩니다. $ emit을 사용해 보셨습니까? 또는 그냥 모의 이벤트 객체를 만들고 메소드에 대해 스파이를 수행하면됩니다. 'expect (event.defaultPrevented) .toBeTruthy();를 테스트 할 필요가 없습니다. 이미 각도 코어의 일부로 테스트되었습니다. – PSL
당신은 정확한 @PSL입니다. 답변으로 추가하십시오. –