Mocking means creating fake versions of database calls to test code without using a real database. It helps check if the code works correctly and quickly.
Click to reveal answer
beginner
Why do we mock database calls in Express apps?
We mock database calls to avoid slow or unreliable real database access during tests. It makes tests faster and safer by isolating the code from the database.
Click to reveal answer
intermediate
Which tool can you use to mock functions in Express tests?
You can use libraries like Sinon.js or Jest to create mocks and spies for database functions in Express tests.
Click to reveal answer
intermediate
How does mocking help when testing error handling in database calls?
Mocking lets you simulate errors from the database, so you can check if your code handles those errors properly without needing a real database failure.
Click to reveal answer
intermediate
What is a stub in mocking database calls?
A stub is a fake function that replaces a real database call and returns preset data or errors to control test scenarios.
Click to reveal answer
What is the main benefit of mocking database calls in Express tests?
AConnects to multiple databases
BImproves database performance
CAutomatically fixes database errors
DFaster and isolated tests without real database
✗ Incorrect
Mocking helps tests run faster and isolates code from the real database.
Which library is commonly used to mock functions in JavaScript tests?
AExpress
BReact
CSinon.js
DMongoDB
✗ Incorrect
Sinon.js is a popular library for mocking and spying on functions.
What does a stub do in mocking?
AReplaces a function with preset responses
BDeletes the database
CRuns the real database query
DCreates a new database
✗ Incorrect
A stub replaces a real function to return controlled data for tests.
How can mocking help test error handling?
ABy ignoring errors
BBy simulating database errors
CBy fixing errors automatically
DBy connecting to a backup database
✗ Incorrect
Mocking can simulate errors so you can check how your code reacts.
When should you avoid mocking database calls?
AWhen you want to test real database integration
BWhen writing unit tests
CWhen testing error handling
DWhen running fast tests
✗ Incorrect
Mocking is not used when you want to test actual database behavior.
Explain how mocking database calls improves testing in Express applications.
Think about speed and control in tests.
You got /4 concepts.
Describe the difference between a mock and a stub when testing database calls.
Focus on purpose and behavior tracking.
You got /4 concepts.
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'); };