差異處
這裏顯示兩個版本的差異處。
Both sides previous revision 前次修改 下次修改 | 前次修改 下次修改 Both sides next revision | ||
web:reactjs:test:teststaticmethod [2019/03/10 17:15] tony |
web:reactjs:test:teststaticmethod [2019/03/10 17:16] tony |
||
---|---|---|---|
行 58: | 行 58: | ||
Auth.signOut = auth_do_nothing.bind(Auth); | Auth.signOut = auth_do_nothing.bind(Auth); | ||
</code> | </code> | ||
- | 但這寫法存在restore的問題,會將狀態延續至下一個testcase中;因此本篇文章將分享透過[[https://sinonjs.org/releases/latest/|Sinon]]去mock static method並且清除mock狀態的做法。 | + | 但這寫法存在會將狀態延續至下一個testcase中(testsuite似乎沒影響);因此本篇文章將分享透過[[https://sinonjs.org/releases/latest/|Sinon]]去mock static method並且清除mock狀態的做法。 |
===== How to? ===== | ===== How to? ===== | ||
- | 以Problem中的Logout為例,我想要模擬使用者點擊Logout並透過Auth.signOut執行登出動作,完整的測試程式碼如下: | + | 以Logout為例,我想要模擬使用者點擊Logout並透過Auth.signOut執行登出動作,完整的測試程式碼如下: |
<code javascript> | <code javascript> | ||
import Logout from '../Logout' | import Logout from '../Logout' | ||
行 102: | 行 102: | ||
}); | }); | ||
</code> | </code> | ||
- | 在test beforeEach中,我透過sinon.stub去模擬Auth.signOut doNothing的動作,即不給予任何的動作;而在afterEach中,我可以透過stub產生的instance去做restore的動作,讓原本Auth.signOut回復到模擬之前: | + | 在test beforeEach中,我透過sinon.stub去模擬Auth.signOut doNothing的動作,即不給予任何的動作;而在afterEach中,我可以透過stub產生的instance去做restore的動作,讓Auth.signOut回復到模擬之前: |
<code javascript> | <code javascript> | ||
let stubSignOut; | let stubSignOut; |