【发布时间】:2017-02-28 18:32:18
【问题描述】:
我正在从article 中学习 TDD,作者谈到了 Mocha 的 beforeEach 将如何在每个断言之前为您运行代码。但我不明白当你可以在描述范围内运行代码时为什么需要这样做。
describe('Test suite for UserComponent', () => {
beforeEach(() => {
// Prevent duplication
wrapper = shallow(<UserComponent
name={ 'Reign' }
age={ 26 } />);
});
it('UserComponent should exist', () => {
expect(wrapper).to.exist;
});
it('Correctly displays the user name and age in paragraphs wrapped under a parent div', () => {
expect(wrapper.type()).to.equal('div');
// more code...
});
});
但不使用 beforeEach 仍然可以工作 -
describe('Test suite for UserComponent', () => {
wrapper = shallow(<UserComponent
name={ 'Reign' }
age={ 26 } />);
it('UserComponent should exist', () => {
expect(wrapper).to.exist;
});
it('Correctly displays the user name and age in paragraphs wrapped under a parent div', () => {
expect(wrapper.type()).to.equal('div');
// more code...
});
});
【问题讨论】:
-
如果要在每次测试前调用ajax请求?或分贝请求?还是清除变量?
标签: javascript unit-testing testing tdd mocha.js