Skip to Content
RESTful Web API Design with Node.js 10 - Third Edition
book

RESTful Web API Design with Node.js 10 - Third Edition

by Valentin Bojinov
April 2018
Intermediate to advanced content levelIntermediate to advanced
178 pages
3h 51m
English
Packt Publishing
Content preview from RESTful Web API Design with Node.js 10 - Third Edition

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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

RESTful Web API Design with Node.js - Second Edition

RESTful Web API Design with Node.js - Second Edition

Valentin Bojinov
Hands-On RESTful API Design Patterns and Best Practices

Hands-On RESTful API Design Patterns and Best Practices

Harihara Subramanian J, Anupama Murali, Pethuru Raj

Publisher Resources

ISBN: 9781788623322Supplemental Content