What's the best way to write unit tests for a REST API?

梦想的初衷 提交于 2020-01-02 02:18:06

问题


When writing unit tests for an API wrapper, should I be making real calls to the REST API endpoints or should I use mocl responses that simulate successful and erroneous calls?


回答1:


Unit tests mean testing only your unit (API wrapper), nothing else. Thus, unfortunately, you should mock the entire API.

On the other hand this never really gives me enough confidence, so I go for system tests (also known as component tests). In that case you should run your API wrapper against existing API, maybe embedded and started along with your test. In integration test, the ultimate scenario, you run your API wrapper against real, but most likely test instance of the API (sandbox, dev environment).

In well established area of database testing: unit tests mock entire DAO level, component tests run against in-memory database while integration tests connect to real database with some fake data.



来源:https://stackoverflow.com/questions/14419155/whats-the-best-way-to-write-unit-tests-for-a-rest-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!