Skip to content

Commit ebe5796

Browse files
committed
Fix constructor bug by no longer using class
Instead the new implementation will return a test double function and place named properties on its prototype. This will allow folks to verify constructor calls just as they would for imitated classes
1 parent 2cb0ed3 commit ebe5796

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

regression/src/constructor-test.coffee

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,17 @@ describe 'td.constructor', ->
6262

6363
describe 'being given an array of function names', ->
6464
Given -> @fakeConstructor = td.constructor(['foo', 'bar'])
65-
Given -> @fakeInstance = new @fakeConstructor()
65+
Given -> @fakeInstance = new @fakeConstructor('biz')
6666
Then -> @fakeConstructor.prototype.foo == @fakeInstance.foo
67+
And -> td.verify(new @fakeConstructor('biz'))
6768
And -> td.explain(@fakeInstance.foo).isTestDouble == true
6869
And -> td.explain(@fakeInstance.bar).isTestDouble == true
69-
And -> @fakeConstructor.toString() == '[test double constructor]'
70+
And -> @fakeConstructor.toString() == '[test double for "(unnamed constructor)"]'
7071
And -> @fakeInstance.toString() == '[test double instance of constructor]'
7172
And -> @fakeInstance.foo.toString() == '[test double for "#foo"]'
7273

74+
75+
7376
describe 'edge case: being given a function without prototypal methods', ->
7477
Given -> @boringFunc = ->
7578
Given -> @boringFunc.foo = ->

src/constructor.js

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,12 @@ export default (typeOrNames) =>
88
: fakeConstructorFromNames(typeOrNames)
99

1010
var fakeConstructorFromNames = (funcNames) => {
11-
return _.tap(class TestDoubleConstructor {}, (cls) => {
11+
return _.tap(td.function('(unnamed constructor)'), (fakeConstructor) => {
12+
fakeConstructor.prototype.toString = () =>
13+
'[test double instance of constructor]'
14+
1215
_.each(funcNames, (funcName) => {
13-
cls.prototype[funcName] = tdFunction(`#${funcName}`)
16+
fakeConstructor.prototype[funcName] = tdFunction(`#${funcName}`)
1417
})
15-
16-
addToStringMethodsToFakeType(cls)
1718
})
1819
}
19-
20-
var addToStringMethodsToFakeType = (fakeType, name) => {
21-
fakeType.toString = () =>
22-
`[test double constructor${name ? ` for "${name}"` : ''}]`
23-
24-
fakeType.prototype.toString = () =>
25-
`[test double instance of constructor${name ? ` "${name}"` : ''}]`
26-
}

0 commit comments

Comments
 (0)