Working with mock objects

Here are the steps that need to be carried out when using mock objects:

  1. Call the require function with sinon as a parameter and export a test function from it:
var sinon = require('sinon'); 
exports.testAPI(test){...} 
  1. Define an API description of the method you want to mock as follows:
var api = {'methodX' : function () {},  
  'methodY' : function() {},  
  'methodZ' : function() {}}; 
  1. Use sinon within the exported function in order to create mock objects out of the api description:
var mock = sinon.mock(api);
  1. Set the expectations on the mock objects. Expectations are set on the mocked objects by describing how the mocked method should behave, what arguments it is supposed to take, and what value it is supposed to ...

Get RESTful Web API Design with Node.js 10 - Third Edition 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.