liveBook · Manning

Mocking

As explained in Functional Programming in JavaScript

A testing technique that replaces external dependencies with controlled stand-ins whose behavior and interactions can be asserted.

Mocking replaces an external dependency with a controlled stand-in, letting tests focus on business logic while checking how the dependency is used. section 6.3

Mocking student lookup

A test mocks the student store, expects get to run once, and returns either null or a valid student object. findStudent can then be tested without relying on a real database.

How Functional Programming in JavaScript covers it

How deep it goes
Demonstrates mock setup, expectations, verification, and restoration, but does not detail mock implementation internals.
Examples
Uses a mocked student database to test findStudent within the showStudent program.
What it assumes
Builds from testing pure functions toward testing functions that depend on impure IO.
What you can do after
Apply it via QUnit and Sinon.JS to simulate database responses and verify dependency interactions.
How it connects
Connects mocking with external dependencies, side effects, pure functions, QUnit, and Sinon.JS.

Also explained in

Compare how 21 books explain mocking →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Programming in JavaScript. The section links point to the parts of the book it draws on.