Javascript 如何测试函数在一段时间后是否发出事件?
我想测试一下柴某事件是否在一段时间后发出。 我的班级:Javascript 如何测试函数在一段时间后是否发出事件?,javascript,node.js,mocha.js,bdd,chai,Javascript,Node.js,Mocha.js,Bdd,Chai,我想测试一下柴某事件是否在一段时间后发出。 我的班级: export default class GeneratorService { constructor() { this.evn = new Events(); this.generate(); } generate() { this.check(); } check() { setTimeout(() => { this.evn.even
export default class GeneratorService {
constructor() {
this.evn = new Events();
this.generate();
}
generate() {
this.check();
}
check() {
setTimeout(() => {
this.evn.events.emit('done', 'value');
}, 2000);
}
}
我不知道如何测试事件done
是否在2秒后发出。通过将一个参数传递给它
(通常称为done
)和
然后在你想完成测试的时候调用这个参数
通过这种方式,您向Mocha发出信号,表明这是一个,并且您希望等待,直到调用done()
完成测试,您将在事件侦听器中对来自GeneratorService
实例的“done”事件执行此测试
下面是一个例子:
const chai = require('chai')
const EventEmitter = require('events').EventEmitter
chai.should()
class Client {
constructor() {
this.evt = new EventEmitter()
}
fireDone() {
setTimeout(() => {
this.evt.emit('done')
}, 2000)
}
}
describe('Client', function () {
// increase mocha's default timeout to 3000ms, otherwise
// the test will timeout after 2000ms.
this.timeout(3000)
const client = new Client()
it('emits done after 2000 ms', function(done) {
const now = Date.now()
client.evt.on('done', function end(value) {
(Date.now() - now).should.be.at.least(2000)
// Do more assertions here; perhaps add tests for `value`.
// Here we call done, signalling to mocha
// that this test is finally over.
done()
// remove listener so it doesn't re-fire on next test.
client.evt.removeListener('done', end)
})
client.fireDone()
})
})
注意:我用Client
替换了GeneratorService
,并且为了简洁起见使其更加紧凑
此外,您可能还可以使用Mocha默认的2000ms超时限制来检查事件是否在2秒内触发,这就不需要添加我在示例中添加的时间比较:(Date.now()-now).should.be.least(2000)
,方法是将一个参数传递给它
(通常称为done
)及
然后在你想完成测试的时候调用这个参数
通过这种方式,您向Mocha发出信号,表明这是一个,并且您希望等待,直到调用done()
完成测试,您将在事件侦听器中对来自GeneratorService
实例的“done”事件执行此测试
下面是一个例子:
const chai = require('chai')
const EventEmitter = require('events').EventEmitter
chai.should()
class Client {
constructor() {
this.evt = new EventEmitter()
}
fireDone() {
setTimeout(() => {
this.evt.emit('done')
}, 2000)
}
}
describe('Client', function () {
// increase mocha's default timeout to 3000ms, otherwise
// the test will timeout after 2000ms.
this.timeout(3000)
const client = new Client()
it('emits done after 2000 ms', function(done) {
const now = Date.now()
client.evt.on('done', function end(value) {
(Date.now() - now).should.be.at.least(2000)
// Do more assertions here; perhaps add tests for `value`.
// Here we call done, signalling to mocha
// that this test is finally over.
done()
// remove listener so it doesn't re-fire on next test.
client.evt.removeListener('done', end)
})
client.fireDone()
})
})
注意:我用Client
替换了GeneratorService
,并且为了简洁起见使其更加紧凑
此外,您可能可以使用Mocha的默认2000ms超时限制来检查事件是否确实在2秒内触发,这就不需要添加我在示例中添加的时间比较:
(Date.now()-now).should.be.least(2000)
看这个答案:可能重复的看这个答案:可能重复的谢谢。这是我的第一个摩卡/柴项目,idk但这是谁的问题。为什么要将err
传递到done()
?它给了我错误bczerr
是未定义的
=error:done(),用非error:value调用;您应该调用done(err)
,并且err
参数的类型为Error
,以防您想向mocha发出信号,表示您想以错误结束此测试(失败)。否则只需done()
。谢谢。这是我的第一个摩卡/柴项目,idk但这是谁的问题。为什么要将err
传递到done()
?它给了我错误bczerr
是未定义的
=error:done(),用非error:value调用;您应该调用done(err)
,并且err
参数的类型为Error
,以防您想向mocha发出信号,表示您想以错误结束此测试(失败)。否则只需done()
。