【发布时间】:2014-11-24 22:50:38
【问题描述】:
所以我将 Reflux 用于我的商店和操作。我有一个应用程序商店,其中包含一些应用程序范围的数据,包括是否启用了防止双击的标志。我的测试看起来像这样(使用 mocha、chai 和 sinon):
var applicationStore = require('../../../../web/app/components/core/application.store.js');
var initialState = _.clone(applicationStore._internalData, true);
function resetToInitialState() {
applicationStore._internalData = _.clone(initialState, true);
}
chai.use(sinonChai);
describe('application store', function() {
beforeEach(function() {
resetToInitialState();
});
it('should have default data set properly', function() {
expect(applicationStore.getPreventDoubleClick()).to.be.false;
});
it('should be able to enable prevent double click flag', function() {
applicationStore._onEnablePreventDoubleClick();
expect(applicationStore.getPreventDoubleClick()).to.be.true;
});
it('should be able to disable prevent double click flag', function() {
applicationStore._onEnablePreventDoubleClick();
applicationStore._onDisablePreventDoubleClick();
expect(applicationStore.getPreventDoubleClick()).to.be.false;
});
});
由于所有商店实际上都是单例的,因此在 beforeEach 中手动重置其内部数据是否是一种有效的测试方法,以确保没有测试影响另一个?有没有更好的方法来使用 mocha/chai/sinon?
【问题讨论】:
标签: unit-testing mocha.js reactjs sinon