![](/img/trans.png)
[英]Reflux state update not showing change of defaultValue of textarea?
[英]Test state change of a reflux store with mocha
我正在針對Reflux商店編寫mocha測試,以驗證操作是否會導致商店中的狀態發生變化。 代碼的縮小版本如下:
商店:
var AppStore = Reflux.createStore({
init: function () {
this.foo = false;
},
listenables: [AppActions],
onFooAction: function() {
this.foo = !this.foo;
this.trigger({action: "foo-ed"});
};
});
行動:
var AppActions = Reflux.createActions([
"fooAction"
]);
測試:
it("toggles foo", function () {
expect(AppStore.foo).to.equal(false);
AppStore.listenables[0].fooAction();
expect(AppStore.foo).to.equal(true);
});
但是,第二個斷言( expect(AppStore.foo).to.equal(true);
)無法說foo
仍為false。
通過在onFooAction
方法中執行console.log
,我已經驗證了該方法實際被觸發並且this.foo
正在切換。
我在這里缺少什么基本概念:概念還是其他? 我真誠地希望這不是一個時間問題!
操作會發出商店偵聽的事件。 基本上,您的測試運行得太快了。
通常,在我的測試中,我假設Reflux將正確地執行它所做的事情,並且我直接調用了listener函數。 您需要添加一些斷言以確保Reflux正確連接。
it("is configured", function () {
expect(AppStore.listenables).to.include(AppActions);
expect(AppActions.fooAction).to.be.a('function');
});
it("toggles foo", function () {
expect(AppStore.foo).to.equal(false);
AppStore.onFooAction();
expect(AppStore.foo).to.equal(true);
});
你可以測試這個的另一種方法是超時,但是當我在測試中加入超時時我會覺得很臟。
it("toggles foo", function (done) {
expect(AppStore.foo).to.equal(false);
AppStore.listenables[0].fooAction();
setTimeout(function () {
try {
expect(AppStore.foo).to.equal(true);
done();
} catch (e) {
done(e);
}
}, 15);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.