0
當運行商店與業力測試&茉莉花(而不是玩笑),商店的狀態持續在測試之間。每次測試重置狀態的最佳方法是什麼?如何重置茉莉花測試之間的Flux商店?
笑話,每個測試都會在自己的環境中自動運行。 This post includes instructions如何清除業務測試之間的需求緩存(複製下面)以模仿開玩笑,但我似乎無法讓它工作。
//tests.webpack.js (suggested by linked article, modified to match my project structure)
// Create a Webpack require context so we can dynamically require our
// project's modules. Exclude test files in this context.
'use strict';
var projectContext = require.context('./path/to/js', true, /^(?!\.test).js$/);
// Extract the module ids that Webpack uses to track modules.
var projectModuleIds = projectContext.keys().map(function (module) {
return String(projectContext.resolve(module));
});
beforeEach(function() {
// Remove our modules from the require cache before each test case.
projectModuleIds.forEach(function (id) {
return delete require.cache[id];
});
});
我當前文件:如果
tests.webpack.js (current)
var context = require.context('./path/to/js', true, /\.test\.js$/);
context.keys().forEach(context);
不知道有幫助,但這裏是從我的測試中,顯示測試之間店裏的狀態持續了代碼的簡化版本。
MissionStore.test.js
describe('MissionStore', function() {
beforeEach(function() {
this.MissionStore = rewire("./MissionStore");
this.registeredCallback = this.MissionStore.__get__("registeredCallback");
});
// A bunch of initialization tests without data
describe("set data", function(){
beforeEach(function(){
// Sets this.MissionStore.getMissions() to some sample data
this.registeredCallback(fetchMissions);
});
it('modifies mission data', function(){
var mission = this.MissionStore.getMissions()[0];
expect(mission.edit).not.toEqual(true);
// modify mission to add an edit flag = true
this.registeredCallback({
actionType: MissionConstants.MISSION_TOGGLE_EDIT_FLAG,
mission : mission
});
expect(this.MissionStore.getMissions()[0].edit).toEqual(true);
});
it('do something else', function(){
expect(this.MissionStore.getMissions()[0].edit).not.toEqual(true); // Failing test
console.log(this.MissionStore.getMissions()[0]);
// prints {..., edit: true}
});
});
});
在'做別的事情的考試,我期待的this.MissionStore狀態測試之間被重置,以便編輯將不等於真了。我會如何去做這件事?
你說得對RegExp是錯誤的,雖然這不是什麼導致商店不被重置。結果用edit = true標誌修改商店中的任務數據正在修改fetchMission數據(它通過引用傳遞)。因此,當它再次運行時,它迫使編輯= true標誌無處不在。儘管感謝您的幫助! –