我测试的文件中的摩卡访问变量
mocha access variable in file which function i test
好的,我会尽量解释这个简单的。所以我有这个文件有这个变量,然后是我正在测试的函数
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();
});
});
这里的问题是,在第一个测试 运行 之后,第二个测试将 return 第一个 if 语句 "Ignoring config event because we already have the config."
因为在第一个测试期间设置了配置。我如何从正在测试函数的文件中访问 let gameConfig
。这样我就可以在每次测试前将其设置为未定义
您可以使用 rewire 重置 config
变量的值。
index.ts
:
type Config = any;
type myEvent = any;
let config: Config | undefined;
function isDefined(obj) {
return obj !== undefined;
}
export default 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;
}
}
index.test.ts
:
import sinon from 'sinon';
import { assert } from 'chai';
import rewire from 'rewire';
type ConfigEvent = any;
const events = { Config: 'Config' };
describe('OnConfigEvent', () => {
let onConfigEvent;
let mod;
beforeEach(() => {
mod = rewire('./');
onConfigEvent = mod.default;
});
afterEach(() => {
mod.__set__({ config: undefined });
});
it('should log missing first thing', () => {
let event: ConfigEvent = {
type: events.Config,
config: { ['firstThing']: false },
};
let spy = sinon.spy(console, 'log');
onConfigEvent(event);
assert(spy.calledWith('config miss 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();
});
});
带有覆盖率报告的单元测试结果:
OnConfigEvent
config miss first thing.
✓ should log missing first thing
config missing second thing.
✓ should log missing second thing
2 passing (1s)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 83.33 | 66.67 | 100 | 83.33 |
index.ts | 83.33 | 66.67 | 100 | 83.33 | 11,12
----------|---------|----------|---------|---------|-------------------
好的,我会尽量解释这个简单的。所以我有这个文件有这个变量,然后是我正在测试的函数
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();
});
});
这里的问题是,在第一个测试 运行 之后,第二个测试将 return 第一个 if 语句 "Ignoring config event because we already have the config."
因为在第一个测试期间设置了配置。我如何从正在测试函数的文件中访问 let gameConfig
。这样我就可以在每次测试前将其设置为未定义
您可以使用 rewire 重置 config
变量的值。
index.ts
:
type Config = any;
type myEvent = any;
let config: Config | undefined;
function isDefined(obj) {
return obj !== undefined;
}
export default 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;
}
}
index.test.ts
:
import sinon from 'sinon';
import { assert } from 'chai';
import rewire from 'rewire';
type ConfigEvent = any;
const events = { Config: 'Config' };
describe('OnConfigEvent', () => {
let onConfigEvent;
let mod;
beforeEach(() => {
mod = rewire('./');
onConfigEvent = mod.default;
});
afterEach(() => {
mod.__set__({ config: undefined });
});
it('should log missing first thing', () => {
let event: ConfigEvent = {
type: events.Config,
config: { ['firstThing']: false },
};
let spy = sinon.spy(console, 'log');
onConfigEvent(event);
assert(spy.calledWith('config miss 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();
});
});
带有覆盖率报告的单元测试结果:
OnConfigEvent
config miss first thing.
✓ should log missing first thing
config missing second thing.
✓ should log missing second thing
2 passing (1s)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 83.33 | 66.67 | 100 | 83.33 |
index.ts | 83.33 | 66.67 | 100 | 83.33 | 11,12
----------|---------|----------|---------|---------|-------------------