You are developing a suite of unit tests for a service that interacts with an external API. How would you employ mocking and stubbing to ensure the tests are isolated and reliable?

  • Mock the external API calls to simulate responses
  • Stub all internal functions used in the service
  • Avoid mocking and use real external API calls
  • Mock only the database interactions
In unit testing, it's crucial to isolate the code being tested. To ensure tests are isolated and reliable when dealing with an external API, you should mock the external API calls. This allows you to control the responses and not rely on external services during tests. Stubbing internal functions is not the primary approach for isolating external API calls. Avoiding mocking by using real API calls can lead to unreliable and slow tests. Mocking the database, in this case, is unnecessary.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *