September 2018
Intermediate to advanced
764 pages
20h 6m
English
We can create different stubs that return different results, each mimicking the possible return values of the create function:
import { stub } from 'sinon';import ValidationError from '../../../validators/errors/validation-error';const createStubs = { success: stub().resolves({ _id: 'foo'}), validationError: stub().rejects(new ValidationError()), otherError: stub().rejects(new Error()),}
Now, if we invoke createStubs.success(), it will always resolve to the { _id: 'foo'} object; therefore, we can use this stub to test for scenarios where the req object we pass into the createUser function is valid. Likewise, we can use createStubs.validationError() to mimic a situation where the req object causes createUser to reject with ...
Read now
Unlock full access