Skip to content

Commit c732f72

Browse files
committed
Merge pull request #18 from testdouble/2-prototype
Test prototype-based create()
2 parents cb75c5a + 23a0f80 commit c732f72

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

lib/create.coffee

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,26 @@ store = require('./store')
33
calls = require('./store/calls')
44
stubbings = require('./store/stubbings')
55

6-
module.exports = (name) ->
6+
module.exports = (nameOrType) ->
7+
if nameOrType?.prototype?
8+
createTestDoublesForEntireType(nameOrType)
9+
else
10+
createTestDouble(nameOrType)
11+
12+
createTestDouble = (name) ->
713
_.tap createTestDoubleFunction(), (testDouble) ->
814
if name? then store.for(testDouble).name = name
915

1016
createTestDoubleFunction = ->
1117
testDouble = (args...) ->
1218
calls.log(testDouble, args, this)
1319
stubbings.get(testDouble, args)
20+
21+
createTestDoublesForEntireType = (type) ->
22+
_.reduce Object.getOwnPropertyNames(type.prototype), (memo, name) ->
23+
memo[name] = if _.isFunction(type.prototype[name])
24+
createTestDouble("#{type.name}##{name}")
25+
else
26+
type.prototype[name]
27+
memo
28+
, {}

test/lib/verify-test.coffee

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ describe '.verify', ->
3434
When -> @result = (shouldThrow => @verify(@testDouble()))
3535
Then -> expect(@result).to.contain("verification on test double `#footime`.")
3636

37+
context 'with a prototype-modeling double', ->
38+
Given -> @SomeType = `function Foo() {}`
39+
Given -> @SomeType::bar = ->
40+
Given -> @SomeType::baz = ->
41+
Given -> @SomeType::biz = "not a function!"
42+
Given -> @testDoubleObj = @create(@SomeType)
43+
When -> @result = (shouldThrow => @verify(@testDoubleObj.baz()))
44+
Then -> expect(@result).to.contain("verification on test double `Foo#baz`.")
45+
Then -> @testDoubleObj.biz == "not a function!"
46+
3747
context 'a double-free verification error', ->
3848
Then -> shouldThrow (=> @verify()), """
3949
No test double invocation detected for `verify()`.

0 commit comments

Comments
 (0)