I've run into a weird problem writing integration tests for my component. When I run each one individually, they pass. When I run multiple, the first one passes and the other ones fail. I think it has something to do with closure actions but I don't know.
我遇到了为我的组件编写集成测试的奇怪问题。当我单独运行每一个时,它们会通过。当我运行多个时,第一个通过而其他通则失败。我认为这与封闭行动有关,但我不知道。
Here's my component code
这是我的组件代码
// components/game-nav-key.js
triggerKeyAction(code) {
if (this.get('prevKeyCode').contains(code)) {
this.sendAction('onPrevKey', true);
} else if (this.get('nextKeyCode').contains(code)) {
this.sendAction('onNextKey', true);
} else if (this.get('openKeyCode').contains(code)) {
this.sendAction('onOpenKey');
}
},
didInsertElement() {
var self = this;
Ember.$('body').keydown(function(e) {
self.triggerKeyAction(e.which);
});
Ember.$('body').keyup(function(e) {
});
}
And my tests
而我的测试
// game-nav-key-test.js
it('tracks key commands and sends an action for K', function() {
let spy = sinon.spy();
this.set('gotoPrev', spy);
this.render(hbs`
{{game-nav-key onPrevKey=(action gotoPrev)}}
`);
triggerKeydown($('body'), 75);
triggerKeyup($('body'), 75);
sinon.assert.calledOnce(spy);
sinon.assert.calledWith(spy, true);
});
it('tracks key commands and sends an action for J', function() {
let spy = sinon.spy();
this.set('gotoNext', spy);
this.render(hbs`
{{game-nav-key onNextKey=(action gotoNext)}}
`);
triggerKeydown($('body'), 74);
triggerKeyup($('body'), 74);
sinon.assert.calledOnce(spy);
sinon.assert.calledWith(spy, true);
});
it('tracks key commands and sends an action for R', function() {
let spy = sinon.spy();
this.set('open', spy);
this.render(hbs`
{{game-nav-key onOpenKey=(action open)}}
`);
triggerKeydown($('body'), 82);
triggerKeyup($('body'), 82);
sinon.assert.calledOnce(spy);
});
I removed all beforeEach's, so it's literally just those three tests. Like I said, each one passes individually, and when it is listed first, but the second two fail when run together. Note that using console.log
statements I have verified that the code hits the line directly above each of the this.sendAction
calls in their respective tests
我删除了所有之前的所有内容,所以它实际上只是那三个测试。就像我说的那样,每一个都是单独传递,当它首先列出时,但是后两个在一起运行时失败。请注意,使用console.log语句我已经验证代码在各自的测试中直接在每个this.sendAction调用的上方命中行
1 个解决方案
#1
1
seems you need to destroy your listeners created in didInsertElement
似乎你需要销毁在didInsertElement中创建的监听器
willDestroyElement() {
Ember.$('body').off('keydown');
Ember.$('body').off('keyup');
}
#1
1
seems you need to destroy your listeners created in didInsertElement
似乎你需要销毁在didInsertElement中创建的监听器
willDestroyElement() {
Ember.$('body').off('keydown');
Ember.$('body').off('keyup');
}