- Mastering React Test:Driven Development
- Daniel Irvine
- 166字
- 2021-06-24 14:45:03
Extracting methods
The call to ReactDOM.render is the same in both methods. Since it's the same in both methods, it makes sense to pull it out.
However, rather than pull it out as-is, we can create a new function that takes the Appointment component as its parameter. This way, we can clearly see how our test data objects are woven through the object under test. If we hid that within an extracted method, the test would be less clear.
The parts of a test that you want to see are the parts that differ between tests. Usually, some data remains the same ( container in this example) and some differs ( customer in this example). Do your best to hide away whatever is the same and proudly display what differs.
Above the first test, write the following definition:
const render = component => ReactDOM.render(component, container);
Now, replace the call to ReactDOM.render in each test with this line:
render(<Appointment customer={customer} />);
Re-run your tests now—they should still be passing.
推薦閱讀
- 從零開始:數字圖像處理的編程基礎與應用
- Python數據可視化:基于Bokeh的可視化繪圖
- Vue.js前端開發基礎與項目實戰
- Production Ready OpenStack:Recipes for Successful Environments
- 高級C/C++編譯技術(典藏版)
- 軟件測試技術指南
- UML 基礎與 Rose 建模案例(第3版)
- Java Web開發詳解
- ArcGIS for Desktop Cookbook
- IDA Pro權威指南(第2版)
- SQL Server 入門很輕松(微課超值版)
- Continuous Delivery and DevOps:A Quickstart Guide Second Edition
- Flink入門與實戰
- C++服務器開發精髓
- Effective C++:改善程序與設計的55個具體做法(第三版)中文版(雙色)