Use a Sinon.JS mock to set expectations around how something was called and how many times prior to execution, and then to validate those expectations afterwards.
- Mocking is an interesting technique…for verifying behavior.…Sinon mocks are used to control…how a unit of code is being used.…Instead of validating, it enforces implementation details…that are set up prior to execution.…A Sinon mock is like a very simple stub…with spy behavior, in that it's watching…how things are being used and gives no responses.…There isn't even an api for creating a response.…A Sinon mock tests how many times a target is called…and with what arguments.…
We're going to use a mock in our application…to ensure the database is being called…only once when saving.…We could go further to check how save…is being called but we'll leave that out…for the sake of time.…Switch over to Atom.…Make sure the test unit…lib reservations is open.…Navigate to the end of the file,…then describe a new context.…So context of save…and then pass it a function.…
We're going to declare a new let dbMock.…Before the test runs, we're going to mock the database.…So dbMock equals…sinon.mockdb.…Close it out with a semicolon,…then after it runs…
- 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
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.