Skip to content

Instantly share code, notes, and snippets.

View mheiber's full-sized avatar

Max Heiber mheiber

View GitHub Profile

Are Mocks a Smell?

It's hard to write unit tests that are worth the costs our employers pay for them. I'll share two mostly-made-up diagrams then use them to justify writing code as data transformation.

Factual Diagrams

There are four types of code:

**Diagram 1: Types of Code**
// Test for Architecture 2
assert.deepEqual(
buildRequest(x, y, z),
expectedRequest
)
assert.deepEqual(
parseResponse(response),
expectedParsedResponse
// Test for Architecture 1
fetch = spy().andMockWith(function (args) {
// pattern matching and pretend games
})
assertSpyStuff
assertMoreSpyStuff
assertMoreSpyStuff
**Diagram 2: Should I unit test?**

                       high
                       ^                ||
                       ^                ||
                       ^  don't test    ||  maybe test
                       ^                ||
cost of testing        ^ ================================
 ^ ||
**Diagram 1: Types of Code**

                       high
                       ^                ||
                       ^                ||
                       ^ boring I/O     ||  interesting I/O
                       ^                ||
cost of testing        ^ ================================
 ^ ||
// Architecture 2
function callApi(x, y, z) {
const request = buildRequest(x, y, z)
return fetch(request)
.then(assertStatus('callApi', x, y, z))
.then(parseResponse)
@mheiber
mheiber / Architecture 1.js
Created January 13, 2018 03:38 — forked from anonymous/Architecture 1.js
Architecture 1.js
// Architecture 1
function callApi(x, y, z) {
manipulate params
manipulate params some more ...
return fetch(...)
.then(result => {
if (result.status === ...) {
handle status stuff
}
@mheiber
mheiber / link-me.sh
Last active October 5, 2017 13:56
run this in the redux repo like this: ./link-me.sh dependency version-number. Example: ./link-me.sh clay-kiln 3.11.0
@mheiber
mheiber / link-me.sh
Created October 5, 2017 13:55
run this in the redux repo like this: ./link-me.sh dependency version-number. Example: ./link-me.sh clay-kiln 3.11.0
@mheiber
mheiber / minimal-behavior.vue
Created May 9, 2017 17:17
Minimal Behavior
<style lang="sass">
</style>
<template>
<input
class="input-text"
@input="update"
:placeholder="args.placeholder"
:value="value"