Skip to content

Instantly share code, notes, and snippets.

Created January 13, 2018 03:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/b2b375c57aa469f9bab4488c40cf2950 to your computer and use it in GitHub Desktop.
Save anonymous/b2b375c57aa469f9bab4488c40cf2950 to your computer and use it in GitHub Desktop.
Figures for Data Transformation is Good and Mocks are Smells
// Architecture 1
function callApi(x, y, z) {
manipulate params
manipulate params some more ...
return fetch(...)
.then(result => {
if (result.status === ...) {
handle status stuff
}
manipulate result
manipulate result some more ...
return manipulatedResult
})
}
// Architecture 2
function callApi(x, y, z) {
const request = buildRequest(x, y, z)
return fetch(request)
.then(assertStatus('callApi', x, y, z))
.then(parseResponse)
**Diagram 1: Types of Code**

                       high
                       ^                ||
                       ^                ||
                       ^ boring I/O     ||  interesting I/O
                       ^                ||
cost of testing        ^ ================================
                       ^                ||
                       ^                ||
                       ^ boring         ||  interesting
                       ^ transformation ||  transformation
                       ^                ||
                       low >>>>>>>>>>>>>>>>>>>>>>>>>>>>> high
                                benefit of testing

**Diagram 2: Should I unit test?**

                       high
                       ^                ||
                       ^                ||
                       ^  don't test    ||  maybe test
                       ^                ||
cost of testing        ^ ================================
                       ^                ||
                       ^                ||
                       ^ maybe test     ||  should test
                       ^                ||
                       ^                ||
                       low >>>>>>>>>>>>>>>>>>>>>>>>>>>>> high
                                benefit of testing
// Test for Architecture 1
fetch = spy().andMockWith(function (args) {
// pattern matching and pretend games
})
assertSpyStuff
assertMoreSpyStuff
assertMoreSpyStuff
// Test for Architecture 2
assert.deepEqual(
buildRequest(x, y, z),
expectedRequest
)
assert.deepEqual(
parseResponse(response),
expectedParsedResponse
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment