我找不到任何工作示例来测试是否发出了事件以及发出的值是否符合预期。
这里是发出消息的类及其父类:
const EventEmitter = require('events').EventEmitter;
class FileHandler extends EventEmitter {
constructor() {
super();
}
canHandle(filePath) {
emit('not super type');
}
parseFile(filePath) {
emit('supper parsing failed');
}
whoAmI() {
return this.emit('who',"FileHandler");
}
}
module.exports = FileHandler;
//diff file
const FileHandler = require('./FileHandler');
class FileHandlerEstedamah extends FileHandler {
constructor() {
super();
}
canHandle(filePath) {
this.emit('FH_check','fail, not my type');
}
parseFile(filePath) {
this.emit('FH_parse','success');
}
}
module.exports = FileHandlerEstedamah;
这是我当前的测试代码:
var sinon = require('sinon');
var chai = require('chai');
const FileHandlerEstedamah = require("../FileHandlerEstedamah");
describe('FH_parse', function() {
it('should fire an FH_parse event', function(){
const fhe = new FileHandlerEstedamah();
var fhParseSpy = sinon.spy();
fhe.on('FH_parse',fhParseSpy);
fhe.parseFile("path");
//I tried a large number of variants of expect, assert, etc to no avail.
});
});
我希望这很简单,但是不知何故我缺少了一些东西。
谢谢你, 詹斯
答案 0 :(得分:2)
您可以断言间谍曾被调用过一次,并以如下所示的预期参数被调用
user.jobs.map(job => (
<div>{job}</div>
))
答案 1 :(得分:0)
您几乎快到了。要检查该值,我们必须在之后的Mocha中调用done()
来告诉我们测试已经完成。
代码
const chai = require('chai');
const assert = chai.assert;
const sinon = require('sinon');
const FileHandlerEstedamah = require("../FileHandlerEstedamah");
describe('FH_parse', function() {
it('should fire an FH_parse event', function(done){ // specify done here
const fhe = new FileHandlerEstedamah();
fhe.on('FH_parse', (data) => {
assert.equal(data, 'success'); // check the value expectation here
done(); // hey mocha, I'm finished with this test
});
fhe.parseFile("path");
});
});
希望有帮助