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.
Loading...
Related Quiz
- What is the difference between a static import and a dynamic import in JavaScript?
- You need to expose a global utility function that should be accessible across different modules in your Node.js application. How would you leverage the global object to achieve this?
- How can you define optional route parameters in Express.js?
- The ______ event of the request object in the http module is emitted when the request body is being received.
- What is the output of the following code snippet: console.log(1 == '1')?