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

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.
主站蜘蛛池模板: 托里县| 乌海市| 西宁市| 思南县| 克拉玛依市| 南城县| 临高县| 舟曲县| 宁陵县| 施秉县| 延安市| 吉木乃县| 三门县| 高安市| 陆丰市| 荥经县| 冕宁县| 民丰县| 石河子市| 麟游县| 朝阳县| 忻州市| 民丰县| 建始县| 黑河市| 阳曲县| 永胜县| 莱西市| 容城县| 晋城| 宝鸡市| 尤溪县| 新营市| 江北区| 策勒县| 甘泉县| 朝阳县| 海丰县| 连山| 丰城市| 习水县|