📖 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 objects
Create a mock function called getUsersFromDb that returns the users array
Create an Express route /users that uses getUsersFromDb to send user data as JSON
Complete 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
1
DATA SETUP: Create mock user data
Create a constant array called users with these exact objects: { id: 1, name: 'Alice' } and { id: 2, name: 'Bob' }.
Express
Hint
Use const users = [ ... ] with two objects inside the array.
2
CONFIGURATION: Create mock database function
Create a function called getUsersFromDb that returns the users array.
Express
Hint
Define a function named getUsersFromDb that returns the users array.
3
CORE LOGIC: Create Express route using mock function
Import Express, create an app with express(), and add a GET route /users that calls getUsersFromDb() and sends the result as JSON with res.json().
Express
Hint
Use require('express') to import Express, create app, and define the GET route with app.get.
4
COMPLETION: Start the Express server
Add app.listen to start the server on port 3000.
Express
Hint
Use app.listen(3000) to start the server on port 3000.
Practice
(1/5)
1. What is the main purpose of mocking database calls in Express applications?
easy
A. To test code without needing a real database connection
B. To speed up the database queries in production
C. To permanently replace the database with fake data
D. To make the database store data faster
Solution
Step 1: Understand mocking concept
Mocking means creating fake versions of parts of your app, like database calls, to test without real dependencies.
Step 2: Identify the purpose in testing
Mocking database calls allows testing code logic without needing a real database, making tests faster and more reliable.
Final Answer:
To test code without needing a real database connection -> Option A
Quick Check:
Mocking = Test without real DB [OK]
Hint: Mocking means faking DB calls to test code fast [OK]
Common Mistakes:
Thinking mocking speeds up production DB
Confusing mocking with permanent DB replacement
Assuming mocking changes real data storage
2. Which of the following is the correct way to mock a database call returning a promise in Express?
easy
A. const mockDbCall = Promise.resolve({ id: 1, name: 'Test' });
getUser returns a promise resolved with an object { id: 5, name: 'Alice' }.
Step 2: Analyze the .then(console.log) call
The .then method receives the resolved value and logs it, so the output is the object itself.
Final Answer:
{ id: 5, name: 'Alice' } -> Option A
Quick Check:
Promise resolves to object logged [OK]
Hint: Promise.then logs resolved value, not the promise itself [OK]
Common Mistakes:
Thinking console.log prints the promise object
Expecting undefined because of missing await
Assuming getUser is undefined or error
4. Identify the error in this mock database call function:
function fetchData() {
return new Promise(data => {
resolve({ id: 1, value: 'mock' });
});
}
medium
A. The function should be async instead of returning a promise
B. The promise executor function parameter should be (resolve, reject), not (data)
C. The data object is missing a required field
D. The function should not return a promise
Solution
Step 1: Review Promise constructor usage
The Promise constructor expects an executor function with two parameters: resolve and reject.
Step 2: Identify misuse of parameter
The code declares executor parameter as 'data' but calls resolve(...), where resolve is undefined. It should declare (resolve, reject) and call resolve(value).
Final Answer:
The promise executor function parameter should be (resolve, reject), not (data) -> Option B
5. You want to mock a database call in Express that sometimes returns data and sometimes throws an error to test error handling. Which mock function correctly simulates this behavior?
hard
A. const mockDb = () => Promise.resolve({ id: 1 });
B. const mockDb = () => { if (Math.random() > 0.5) return { id: 1 }; else throw new Error('DB error'); };