在Flux架构中测试商店

时间:2014-11-24 22:50:39

标签: unit-testing mocha reactjs sinon

所以我在我的商店和行动中使用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有更好的方法吗?

1 个答案:

答案 0 :(得分:-1)

由于我使用JestJS,我没有遇到过这个问题。它似乎处理单身非常好

我假设,建议切换测试框架是不可能的。

如果是这种情况,那么您的设置似乎没问题。我的另一个想法是让它与商店内联。

var _internalData = false;

var applicationStore = {
    reset: function() {
        _internalData = false;
    }
};

然后你可以调用applicationStore.reset()来重置它,并测试重置功能。<​​/ p>

两者都不是理想的。