Simulating behavior with stubs

Stubs are functions that simulate the behavior of another component.

In Sinon, stubs are an extension to spies; this means that all the methods that are available to spies are also available to stubs.

In the context of our tests, we don't really care about the returned value ores.json() – we only care that our checkEmptyPayload middleware function relays this value back faithfully. Therefore, we can turn our res.json spy into a stub, and make it return a reference to an object:

resJsonReturnValue = {};res = {  status: spy(),  set: spy(),  json: stub().returns(resJsonReturnValue),};

We can then add another assertion step to compare the value returned by the checkEmptyPayload function, and the value returned by ...

Get Building Enterprise JavaScript Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.