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

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.
主站蜘蛛池模板: 武汉市| 武宣县| 手游| 石柱| 湖南省| 横峰县| 遂昌县| 长武县| 山东省| 富阳市| 德令哈市| 贡嘎县| 长阳| 驻马店市| 城步| 台中市| 宿州市| 三亚市| 临泽县| 乌鲁木齐市| 郓城县| 金沙县| 静海县| 河津市| 万宁市| 舟山市| 阿鲁科尔沁旗| 资阳市| 溆浦县| 东乌珠穆沁旗| 长子县| 清河县| 炉霍县| 淳安县| 施甸县| 岑溪市| 新乐市| 福贡县| 齐齐哈尔市| 吉安市| 科技|