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

Mocking

In order for tests to run fast 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. Speed of the 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 tests suite execution should be measured in minutes, if not seconds. Designing the code in a way that it can be easily mocked and stubbed, forces us to better structure that code by applying separation of concerns.

More important than speed is the benefit of 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.

Tip

With or without mocks, the code should be written in a way that we can easily replace one dependency with another.

主站蜘蛛池模板: 临西县| 尉犁县| 乐清市| 青川县| 阿拉善盟| 米泉市| 故城县| 德格县| 板桥市| 元氏县| 辽阳市| 建阳市| 沭阳县| 乃东县| 彭泽县| 吴江市| 宁国市| 烟台市| 涿鹿县| 陵水| 谢通门县| 广河县| 安龙县| 宣威市| 长宁县| 铁岭市| 北流市| 绥江县| 蕲春县| 太和县| 新绛县| 祁东县| 神池县| 郸城县| 阜康市| 凌源市| 平武县| 德令哈市| 徐汇区| 米脂县| 临漳县|