【发布时间】:2016-02-24 11:33:42
【问题描述】:
我正在使用 React、Facebook 的 Dispatcher 和 Flux Utils 构建一个应用程序,我正在使用 karma、mocha、sinon、browserify 来测试 Flux 商店。这是我用于测试的 sn-p:
import {expect} from 'chai';
import sinon from 'sinon';
describe('Store', function() {
let registerSpy, Store, registeredCallback, isDispatchingStub;
beforeEach(function(done) {
const Dispatcher = require('../../../dispatcher');
registerSpy = sinon.spy(Dispatcher, 'register');
Store = require('../store');
registeredCallback = registerSpy.lastCall.args[0];
isDispatchingStub
= sinon.stub(Dispatcher, "isDispatching", function() { return true; });
done();
});
afterEach(function(done) {
registerSpy.restore();
isDispatchingStub.restore();
done();
});
it('should update its state on message', function(done) {
registeredCallback({
type: 'MESSAGE',
payload: {
message: 'Our message'
}
});
let state = Store.getState();
expect(state.size).to.equal(1);
done();
});
});
如果我只执行一个测试,一切正常。如果我添加另一个测试,我会收到一个错误,因为商店在需要时没有重新加载(我没有得到商店的新副本,在之前的测试之后它已经有了一些状态)。因此,registerSpy 没有 lastCall 属性,因为调度程序已经注册了商店。
Dispatcher也是一样,其他store可以看到dispatch的action,所以要正确测试store很麻烦。
我尝试过使用rewire,但它不支持browserify。我也尝试过取消require.cache,但 Browserify 的 require 没有 node 的缓存属性。
如何使用 browserify 获取模块的新副本?
【问题讨论】:
标签: reactjs mocha.js browserify sinon flux