0
0
Expressframework~10 mins

Mocking database calls in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Mocking database calls
Start Express app
Receive API request
Call database function
Mock intercepts call
Return fake data
Send response with fake data
End request
This flow shows how an Express app receives a request, calls a database function, but the call is intercepted by a mock that returns fake data instead of real database results.
Execution Sample
Express
const express = require('express');
const app = express();

// Mock database function
const db = { getUser: () => ({ id: 1, name: 'Mock User' }) };

app.get('/user', (req, res) => {
  const user = db.getUser();
  res.json(user);
});
This code sets up an Express route that returns a mocked user object instead of calling a real database.
Execution Table
StepActionFunction CalledReturn ValueResponse Sent
1Receive GET /user requestN/AN/AN/A
2Call db.getUser()db.getUser(){ id: 1, name: 'Mock User' }N/A
3Send JSON responseN/AN/A{ id: 1, name: 'Mock User' }
4Request endsN/AN/AN/A
💡 Request ends after sending mocked user data as JSON response
Variable Tracker
VariableStartAfter Step 2Final
userundefined{ id: 1, name: 'Mock User' }{ id: 1, name: 'Mock User' }
Key Moments - 2 Insights
Why does the app return a user object without connecting to a real database?
Because the db.getUser function is mocked to return a fixed object, as shown in execution_table step 2, so no real database call happens.
What happens if we remove the mock and call a real database function?
The app would try to connect to the database, which might delay response or fail if the database is unavailable. The mock avoids this by returning data immediately.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'user' after step 2?
Aundefined
B{ id: 1, name: 'Mock User' }
Cnull
D{}
💡 Hint
Check the variable_tracker table under 'After Step 2' for 'user'
At which step is the JSON response sent back to the client?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Response Sent' column in execution_table
If the mock was removed, what would change in the execution flow?
AThe response would be sent before calling db.getUser
BThe app would not receive the request
Cdb.getUser would call the real database instead of returning fake data
DThe mock would still intercept the call
💡 Hint
Refer to key_moments about what happens without the mock
Concept Snapshot
Mocking database calls in Express:
- Replace real DB functions with fake ones returning fixed data
- Allows testing API routes without real DB
- Mocked functions return immediately
- Helps isolate backend logic from DB
- Use mocks during development and testing
Full Transcript
This example shows how an Express app handles a GET request to /user by calling a mocked database function. Instead of querying a real database, the mock returns a fixed user object immediately. The app then sends this mocked data as a JSON response. This approach helps developers test and develop APIs without needing a real database connection. The execution table traces each step: receiving the request, calling the mock, returning fake data, and sending the response. The variable tracker shows the user variable getting assigned the mocked data. Key moments clarify why mocking avoids real database calls and what changes if the mock is removed. The quiz tests understanding of the flow and variable states.