Javascript 检查是否使用mocha触发事件
如何测试元素是否使用mocha触发事件?我已经有了一个丑陋的解决方案,但它不是非常可读,失败时需要很长时间超时,并且没有提供良好的失败消息Javascript 检查是否使用mocha触发事件,javascript,testing,polymer,mocha.js,web-component-tester,Javascript,Testing,Polymer,Mocha.js,Web Component Tester,如何测试元素是否使用mocha触发事件?我已经有了一个丑陋的解决方案,但它不是非常可读,失败时需要很长时间超时,并且没有提供良好的失败消息 describe('test-element', function() { var el; beforeEach(function() { el = document.createElement('test-element'); }); it('fires a save event', function(done) { e
describe('test-element', function() {
var el;
beforeEach(function() {
el = document.createElement('test-element');
});
it('fires a save event', function(done) {
el.addEventListener('save', function() {
done();
});
el.save();
});
在一个完美的世界里,我认为这样的事情会更酷
it('fires a save event', function() {
el.save();
expect(el).to.have.firedEvent('save');
});
});
我这样做对吗?我应该使用更好的方法或自定义匹配器库吗?如何监视
fire
函数
不确定您正在使用的存根/间谍库是什么,但可以这样说。所以有点像
var spy = sinon.spy(el, 'fire');
el.save();
expect(spy.calledWith('save')).to.be.true;