Mocking database calls in Express
📖 Scenario: You are building a simple Express server that fetches user data from a database. To test your server without a real database, you will mock the database calls.
🎯 Goal: Create an Express server with a mocked database call that returns user data. You will set up the data, configure a mock function, implement the route using the mock, and complete the server setup.
📋 What You'll Learn
Create a mock user data array called
users with exact user objectsCreate a mock function called
getUsersFromDb that returns the users arrayCreate an Express route
/users that uses getUsersFromDb to send user data as JSONComplete the Express server setup with
app.listen on port 3000💡 Why This Matters
🌍 Real World
Mocking database calls helps developers test server routes without needing a real database connection. This speeds up development and testing.
💼 Career
Backend developers often mock database calls to write unit tests and develop APIs before the database is ready or to isolate code during testing.
Progress0 / 4 steps