Override a database response using a Sinon.JS stub to return arbitrary values to test how a response is handled by the tested code.
- [Instructor] The process of stubbing a custom response…takes a couple steps, but it's straightforward.…You'll need to replicate how the method responds.…Is it returning a value, throwing an exception,…or is it a promise that resolves or rejects?…Once you know how the response will be made…you'll need to replicate what is being returned.…The goal should be to match the structure.…If it returns an integer,…the stub will need to return an integer, and so forth.…The easiest way to discover this is to console.log…the actual response if the API documentation…isn't available or you're not sure.…
How can we use a wrapped stub?…In our application, the best target will be create,…which uses save to insert a reservations record…into the database.…Create uses that response to return an ID.…To do this, we'll wrap the database, replace the run method,…and return a resolved promise…that has the correct structure.…Let's make this happen.…Switch over to Atom.…Make sure that you have test, unit,…lib, reservations.js open.…
At the top of the file,…
- What is code quality?
- Testing and code quality fundamentals
- Coding conventions and standards
- Creating and enforcing coding standards
- Unit, integration, and functional testing
- Test-driven development test specificatons
- Behavior-driven development test specifications
- Finding errors with linting
- Extending an ESLint shareable config
- Validating correctness with unit testing
- Replacing and inspecting with stubs, spies, and mocks
- Code coverage and why it matters
- Coverage with continuous integration
Skill Level Intermediate
Node.js: Build Your First Microservicewith Daniel Khan2h 9m Intermediate
Building APIs Using Hapi in Node.jswith Kirsten Hunter2h 23m Intermediate
Node.js: Extend and Maintain Applicationswith Daniel Khan3h 35m Intermediate
1. Testing and Code Quality Fundamentals
2. Finding Errors with Linting
3. Validating Correctness with Unit Testing
4. Replacing and Inspecting with Stubs, Spies, and Mocks
5. Reporting on Your Entire Codebase
Where to go from here?3m 46s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.