Skip to content

Commit

Permalink
Step 9: refactor ensureDemonstration to "pop"
Browse files Browse the repository at this point in the history
In general, when I can remove a logical branch from a delegator unit
(this unit is said to be a "delegator" because its primary task is 
offloading work onto other units), I'll usually rework the naming to 
push the branch down. Therefore, "ensureDemonstration" became 
"popDemonstration", and instead of simply providing a null-check and a
potential error, it will now pop the CallLog itself and blow up as 
necessary.

Does it really matter whose job that is? No, not really. But is the 
resulting `verify` module cleaner? Absolutely! It no longer mixes levels
of abstraction quite so much (notice how before it was a little odd how
the unit had awareness of a repository object as well as 5 business 
logic modules)

This refactor is a good example of the kind of thought process that
discovery testing encourages which I typically would never have 
undergone had I just written the code (or even "classical TDD'd" it)
  • Loading branch information
searls committed Dec 23, 2017
1 parent d0afa13 commit ef2c419
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 15 deletions.
2 changes: 0 additions & 2 deletions src/verify/ensure-demonstration.js

This file was deleted.

7 changes: 2 additions & 5 deletions src/verify/index.js
@@ -1,14 +1,11 @@
import CallLog from '../value/call-log'

import ensureDemonstration from './ensure-demonstration'
import popDemonstration from './pop-demonstration'
import didCallOccur from './did-call-occur'
import notifySatisfiedMatchers from './notify-satisfied-matchers'
import warnIfAlsoStubbed from './warn-if-also-stubbed'
import fail from './fail'

export default function verify (__userInvokesDemonstrationHere__, config) {
const {double, call} = CallLog.instance.pop() || {}
ensureDemonstration(call)
const {double, call} = popDemonstration()
if (didCallOccur(double, call, config)) {
notifySatisfiedMatchers(double, call, config)
warnIfAlsoStubbed(double, call, config)
Expand Down
2 changes: 2 additions & 0 deletions src/verify/pop-demonstration.js
@@ -0,0 +1,2 @@
export default function popDemonstration () {
}
13 changes: 5 additions & 8 deletions test/unit/verify/index.test.js
@@ -1,12 +1,11 @@
import Double from '../../../src/value/double'
import Call from '../../../src/value/call'
import CallLog from '../../../src/value/call-log'

let subject, ensureDemonstration, didCallOccur, notifySatisfiedMatchers,
let subject, popDemonstration, didCallOccur, notifySatisfiedMatchers,
warnIfAlsoStubbed, fail
module.exports = {
beforeEach: () => {
ensureDemonstration = td.replace('../../../src/verify/ensure-demonstration').default
popDemonstration = td.replace('../../../src/verify/pop-demonstration').default
didCallOccur = td.replace('../../../src/verify/did-call-occur').default
notifySatisfiedMatchers = td.replace('../../../src/verify/notify-satisfied-matchers').default
warnIfAlsoStubbed = td.replace('../../../src/verify/warn-if-also-stubbed').default
Expand All @@ -16,34 +15,32 @@ module.exports = {
'verified to have occurred as configured': () => {
const double = new Double()
const call = new Call()
CallLog.instance.log(double, call)
const config = {some: 'option'}
td.when(popDemonstration()).thenReturn({double, call})
td.when(didCallOccur(double, call, config)).thenReturn(true)

subject(/*imagine double('a','b','c')*/ undefined, config)

td.verify(ensureDemonstration(call))
td.verify(notifySatisfiedMatchers(double, call, config))
td.verify(warnIfAlsoStubbed(double, call, config))
assert.equal(td.explain(fail).callCount, 0)
},
'demonstrated call DID NOT occur, failing test': () => {
const double = new Double()
const call = new Call()
CallLog.instance.log(double, call)
const config = {some: 'option'}
td.when(popDemonstration()).thenReturn({double, call})
td.when(didCallOccur(double, call, config)).thenReturn(false)

subject(/*imagine double('a','b','X')*/ undefined, config)

td.verify(ensureDemonstration(call))
td.verify(fail(double, call, config))
assert.equal(td.explain(notifySatisfiedMatchers).callCount, 0)
assert.equal(td.explain(warnIfAlsoStubbed).callCount, 0)
},
'demonstration missing blows up': () => {
const config = {some: 'option'}
td.when(ensureDemonstration(undefined)).thenThrow(new Error('wups'))
td.when(popDemonstration()).thenThrow(new Error('wups'))

assert.throws(() => {
subject(/*imagine double('a','b','X')*/ undefined, config)
Expand Down

0 comments on commit ef2c419

Please sign in to comment.