Tôi đang cố gắng kiểm tra đơn vị chức năng liên kết với chỉ thị ngClick. Nó trông giống như thế này bây giờ khi chúng ta vừa mới bắt đầu vào dự án này và trước khi tôi nhận được đến nay tôi muốn có một số bảo hiểm thử nghiệm:
vm.open = function($event) {
$event.preventDefault();
$event.stopPropagation();
vm.opened = true;
};
tôi kiểm tra đơn vị như thế này:
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();
});
});
Khi Karma chạy thử nghiệm tôi nhận được lỗi này:
TypeError: $event.stopPropagation is not a function.
Bất kỳ ý tưởng nào?
Bạn đã thử '$ ($ event) .stopPropagation();'? – algorhythm
Khi bạn phát sự kiện, không có tuyên truyền dừng nào phải không? phát sóng đi xuống, stopPropagation là cho phát ra. Bạn đã thử sử dụng $ emit. hoặc chỉ cần tạo một đối tượng sự kiện giả và chỉ cần theo dõi các phương thức. Bạn không thực sự cần kiểm tra 'expect (event.defaultPrevented) .toBeTruthy();' nó đã được thử nghiệm như một phần của lõi góc. – PSL
bạn chính xác @PSL, vui lòng thêm làm câu trả lời và tôi sẽ chấp nhận –