Testing a fluent/chained API with testdouble.js

node v6.17.1
version: master
endpointsharetweet
/* I'm very wary of the utility of a test like this, because * it appears that `applyFilters` is a pure function and should * therefore be tested without a need for any test doubles (rather, * passing it values and evaluating the result should suffice). * * However, if you _were_ going to test a method that delegated * to a chained API, here is one way you could do it: */ // Subject function applyFilters(query, filters) { for (const key in filters) { query = query.where(key, '=', filters[key]) } return query } // Test const td = require('testdouble') const assert = require('assert') const query1 = td.object(['where']) const query2 = td.object(['where']) const query3 = td.object(['where']) td.when(query1.where('a', '=', 1)).thenReturn(query2) td.when(query2.where('b', '=', 'x')).thenReturn(query3) td.when(query3.where('c', '=', null)).thenReturn('huzzah!') const result = applyFilters(query1, { a: 1, b: 'x', c: null }) assert.equal(result, 'huzzah!')
Loading…

no comments

    sign in to comment