Complete the code to import the mocking library for testing database calls.
const [1] = require('sinon');
The sinon library is commonly used to mock functions and database calls in Express apps.
Complete the code to stub the database method findUser to return a fake user.
const stub = sinon.[1](db, 'findUser').returns({ id: 1, name: 'Test User' });
The stub method replaces the original function with a fake one that returns specified data.
Fix the error in restoring the stub after the test to avoid side effects.
stub.[1]();The restore method returns the original function to its normal state after stubbing.
Fill both blanks to create a mock Express route that uses the stubbed database call and sends a JSON response.
app.get('/user', async (req, res) => { const user = await db.[1](); res.[2](user); });
The route calls the stubbed findUser method and sends the user data as JSON with res.json().
Fill all three blanks to write a test that stubs the database call, calls the route handler, and asserts the response.
const stub = sinon.[1](db, 'findUser').returns({ id: 2, name: 'Mocked' }); await request(app).get('/user').expect(200).expect(res => { if (res.body.name !== [2]) throw new Error('Wrong user'); }); stub.[3]();
The test stubs findUser, expects the response to have the mocked name, and restores the stub after.