- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在尝试对绑定(bind)到 ngClick 指令的函数进行单元测试。现在看起来像这样,因为我们刚刚开始这个项目,在我开始之前我想要一些测试覆盖率:
vm.open = function($event) {
$event.preventDefault();
$event.stopPropagation();
vm.opened = true;
};
我这样进行单元测试:
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();
});
});
当 Karma 运行测试时我得到这个错误:
TypeError: $event.stopPropagation is not a function.
有什么想法吗?
最佳答案
您的问题是 $broadcasted
上没有stopPropagation
方法 事件。 broadcast 向下传播,而 stopPropagation(在 $emit
中可用)用于防止进一步向上传播。所以你有两个选择。
要么使用$emit
it('should set vm.opened to true', function(){
event = scope.$emit("click");
expect(event).toBeDefined();
scope.vm.open(event);
expect(event.defaultPrevented).toBeTruthy();
expect(scope.vm.opened).toBeTruthy();
});
或者只是为事件创建一个模拟对象。
it('should set vm.opened to true', function(){
event = jasmine.createSpyObj('event', ['preventDefault', 'stopPropagation']);
scope.vm.open(event);
expect(event.preventDefault).toHaveBeenCalled();
expect(scope.vm.opened).toBeTruthy();
});
另请注意,您真的不需要测试 expect(event.defaultPrevented).toBeTruthy();
或 expect(event).toBeDefined();
因为这是调用 preventDefault 时的核心 Angular 功能,并且已经过测试。
关于javascript - $event.stopPropogation 不是 Angularjs 单元测试中的函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26512291/
HTML 代码: List_text jQuery 函数: $("#list_id").live('click' , function(){ alert('List is clicked');
我正在尝试对绑定(bind)到 ngClick 指令的函数进行单元测试。现在看起来像这样,因为我们刚刚开始这个项目,在我开始之前我想要一些测试覆盖率: vm.open = function($even
我试图阻止按钮点击冒泡到表格行事件,但我似乎无法阻止它发生。 所以,基本上,我有一个表格行,它是可点击的,它会将您带到不同的屏幕。 我在行上有一个按钮,它执行下面的函数,并通过 AJAX 返回数据,并
我刚刚使用 jquery 构建了一个自定义自动完成组件。我的问题是,当有人使用键盘上/下箭头键选择自动完成建议之一并按回车键时,即使我调用 event.stopPropogation() 和 even
我是一名优秀的程序员,十分优秀!