【发布时间】:2020-07-04 00:43:57
【问题描述】:
好的,我将尝试简单地解释一下。所以我有这个文件,它有这个变量,然后是我正在测试的函数
let config: Config|undefined;
export default async function onConfigEvent(event: myEvent) {
if (isDefined(config)) {
console.log('Ignoring config event because we already have the config.');
return;
}
config = event.config as Config;
if (!config.firstThing) {
console.log('config miss first thing.')
return;
}
if (!config.otherthing) {
console.log('config missing second thing.');
return;
}
}
然后我尝试测试这两个是否是这样的
describe('OnConfigEvent', () => {
it('should log missing second thing', () => {
let event: ConfigEvent = {
type: events.Config,
config: { ["firstThing"]: false }
}
let spy = sinon.spy(console, 'log');
onConfigEvent(event)
assert(spy.calledWith('Missing first thing.'));
spy.restore();
});
it('should log missing second thing', () => {
let event: ConfigEvent = {
type: events.Config,
config: { ["firstThing"]: true }
}
let spy = sinon.spy(console, 'log');
onConfigEvent(event)
assert(spy.calledWith('config missing second thing.'));
spy.restore();
});
});
这里的问题是,在第一次测试运行后,第二次测试将返回第一个 if 语句 "Ignoring config event because we already have the config.",因为在第一次测试期间设置了配置。我如何从我正在测试函数的文件中访问let gameConfig。所以我可以在每次测试之前将其设置为未定义
【问题讨论】:
标签: javascript testing mocha.js sinon