官术网_书友最值得收藏!

Mocking

In order for tests to run quickly and provide constant feedback, code needs to be organized in such a way that the methods, functions, and classes can be easily replaced with mocks and stubs. A common word for this type of replacements of the actual code is test double. The speed of execution can be severely affected with external dependencies; for example, our code might need to communicate with the database. By mocking external dependencies, we are able to increase that speed drastically. Whole unit test suite execution should be measured in minutes, if not seconds. Designing the code in a way that can be easily mocked and stubbed forces us to structure that code better by applying a separation of concerns.

More important than speed is the benefit of the removal of external factors. Setting up databases, web servers, external APIs, and other dependencies that our code might need, is both time consuming and unreliable. In many cases, those dependencies might not even be available. For example, we might need to create a code that communicates with a database and have someone else create a schema. Without mocks, we would need to wait until that schema is set.


With or without mocks, the code should be written in such a way that we can easily replace one dependency with another.
主站蜘蛛池模板: 太仆寺旗| 隆尧县| 黑山县| 武汉市| 福贡县| 张家界市| 仙桃市| 乳山市| 永康市| 华安县| 乌恰县| 梧州市| 雅江县| 洪泽县| 阿巴嘎旗| 仲巴县| 宝丰县| 肇东市| 开鲁县| 雷波县| 浮山县| 麻江县| 湘潭市| 昌江| 师宗县| 云和县| 根河市| 廉江市| 库车县| 盱眙县| 浠水县| 尼勒克县| 威海市| 洞口县| 罗甸县| 喀喇| 南阳市| 蒙自县| 昌黎县| 明水县| 锡林郭勒盟|