Skip to content

Commit cb75c5a

Browse files
committed
Merge pull request #17 from testdouble/name-td
Allow naming test doubles for better output
2 parents 6118d2a + 6ec80e1 commit cb75c5a

File tree

6 files changed

+44
-13
lines changed

6 files changed

+44
-13
lines changed

lib/create.coffee

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
_ = require('lodash')
2+
store = require('./store')
13
calls = require('./store/calls')
24
stubbings = require('./store/stubbings')
35

4-
module.exports = ->
5-
testDouble = (args...) -> #<-- return a test double
6+
module.exports = (name) ->
7+
_.tap createTestDoubleFunction(), (testDouble) ->
8+
if name? then store.for(testDouble).name = name
9+
10+
createTestDoubleFunction = ->
11+
testDouble = (args...) ->
612
calls.log(testDouble, args, this)
713
stubbings.get(testDouble, args)

lib/explain.coffee

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
_ = require('lodash')
22

3+
store = require('./store')
34
callsStore = require('./store/calls')
45
stubbingsStore = require('./store/stubbings')
56
stringifyArgs = require('./stringify-args')
@@ -11,7 +12,7 @@ module.exports = (testDouble) ->
1112
callCount: calls.length
1213
calls: calls
1314
description: """
14-
This test double has #{stubs.length} stubbings and #{calls.length} invocations.
15+
This test double #{stringifyName(testDouble)}has #{stubs.length} stubbings and #{calls.length} invocations.
1516
""" + stubbingDescription(stubs) + callDescription(calls)
1617

1718
stubbingDescription = (stubs) ->
@@ -26,4 +27,8 @@ callDescription = (calls) ->
2627
desc + "\n - called with `(#{stringifyArgs(call.args)})`."
2728
, "\n\nInvocations:"
2829

29-
30+
stringifyName = (testDouble) ->
31+
if name = store.for(testDouble).name
32+
"`#{name}` "
33+
else
34+
""

lib/verify.coffee

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
_ = require('lodash')
2+
store = require('./store')
23
callsStore = require('./store/calls')
34
stringifyArgs = require('./stringify-args')
45

@@ -10,15 +11,15 @@ module.exports = ->
1011
throw new Error(unsatisfiedErrorMessage(last.testDouble, last.args))
1112
else
1213
throw new Error """
13-
No test double invocation call detected for `verify()`.
14+
No test double invocation detected for `verify()`.
1415
1516
Usage:
1617
verify(myTestDouble('foo'))
1718
"""
1819

1920
unsatisfiedErrorMessage = (testDouble, args) ->
2021
"""
21-
Unsatisfied test double verification.
22+
Unsatisfied verification on test double#{stringifyName(testDouble)}.
2223
2324
Wanted:
2425
- called with `(#{stringifyArgs(args)})`.
@@ -32,3 +33,9 @@ invocationSummary = (testDouble) ->
3233
_.reduce calls, (desc, call) ->
3334
desc + "\n - called with `(#{stringifyArgs(call.args)})`."
3435
, "\n\n But was actually called:"
36+
37+
stringifyName = (testDouble) ->
38+
if name = store.for(testDouble).name
39+
" `#{name}`"
40+
else
41+
""

test/helper.coffee

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ global.xThen = ->
77
global.shouldNotThrow = (func) ->
88
func()
99

10-
global.shouldThrow = (func, message) ->
10+
global.shouldThrow = (func, expectedMessage) ->
1111
threw = null
12+
actualMessage = null
1213
try
1314
func()
1415
threw = false
1516
catch e
16-
expect(e.message).to.eq(message) if message?
17+
actualMessage = e.message
18+
expect(actualMessage).to.eq(expectedMessage) if expectedMessage?
1719
threw = true
1820
expect(threw, "Expected function to throw an error").to.be.true
19-
21+
actualMessage
2022

test/lib/explain-test.coffee

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ describe '.explain', ->
1414
This test double has 0 stubbings and 0 invocations.
1515
"""
1616

17+
context 'a named test double', ->
18+
Given -> @testDouble = @create("foobaby")
19+
Then -> expect(@result.description).to.deep.eq """
20+
This test double `foobaby` has 0 stubbings and 0 invocations.
21+
"""
22+
1723
context 'a double with some interactions', ->
1824
Given -> @when(@testDouble(88)).thenReturn(5)
1925
Given -> @when(@testDouble("two things!")).thenReturn("woah", "such")

test/lib/verify-test.coffee

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe '.verify', ->
99

1010
context 'an unsatisfied verification - no interactions', ->
1111
Then -> shouldThrow (=> @verify(@testDouble("WOAH"))), """
12-
Unsatisfied test double verification.
12+
Unsatisfied verification on test double.
1313
1414
Wanted:
1515
- called with `("WOAH")`.
@@ -20,7 +20,7 @@ describe '.verify', ->
2020
context 'unsatisfied verify - other interactions', ->
2121
When -> @testDouble("the wrong WOAH")
2222
Then -> shouldThrow (=> @verify(@testDouble("WOAH"))), """
23-
Unsatisfied test double verification.
23+
Unsatisfied verification on test double.
2424
2525
Wanted:
2626
- called with `("WOAH")`.
@@ -29,9 +29,14 @@ describe '.verify', ->
2929
- called with `("the wrong WOAH")`.
3030
"""
3131

32-
context 'a double-free verification', ->
32+
context 'with a named double', ->
33+
Given -> @testDouble = @create("#footime")
34+
When -> @result = (shouldThrow => @verify(@testDouble()))
35+
Then -> expect(@result).to.contain("verification on test double `#footime`.")
36+
37+
context 'a double-free verification error', ->
3338
Then -> shouldThrow (=> @verify()), """
34-
No test double invocation call detected for `verify()`.
39+
No test double invocation detected for `verify()`.
3540
3641
Usage:
3742
verify(myTestDouble('foo'))

0 commit comments

Comments
 (0)