Skip to content

Commit 1ca7fc1

Browse files
committed
Improve error message with invalid td.object call
Fixes #224
1 parent c54b741 commit 1ca7fc1

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/object.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ var fakeObject = (nameOrType, config) => {
1717
return createTestDoublesForPlainObject(nameOrType)
1818
} else if (_.isArray(nameOrType)) {
1919
return createTestDoublesForFunctionNames(nameOrType)
20+
} else if (_.isString(nameOrType) || nameOrType === undefined) {
21+
return createTestDoubleViaProxy(nameOrType, withDefaults(config))
2022
} else if (_.isFunction(nameOrType)) {
21-
return ensureFunctionIsNotPassed()
23+
ensureFunctionIsNotPassed()
2224
} else {
23-
return createTestDoubleViaProxy(nameOrType, withDefaults(config))
25+
ensureOtherGarbageIsNotPassed()
2426
}
2527
}
2628

@@ -63,6 +65,20 @@ Did you mean \`td.object(['${name}'])\`?\
6365
}
6466
}
6567

68+
var ensureFunctionIsNotPassed = () =>
69+
log.error('td.object', `Functions are not valid arguments to \`td.object\` (as of [email protected]). Please use \`td.function()\` or \`td.constructor()\` instead for creating fake functions.`)
70+
71+
var ensureOtherGarbageIsNotPassed = () =>
72+
log.error('td.object', `\
73+
To create a fake object with td.object(), pass it a plain object that contains
74+
functions, an array of function names, or (if your runtime supports ES Proxy
75+
objects) a string name.
76+
77+
If you passed td.object an instance of a custom type, consider passing the
78+
type's constructor to \`td.constructor()\` instead.
79+
`)
80+
81+
6682
var withDefaults = (config) =>
6783
_.extend({}, DEFAULT_OPTIONS, config)
6884

@@ -75,6 +91,3 @@ var nameOf = (nameOrType) =>
7591
_.isString(nameOrType)
7692
? nameOrType
7793
: ''
78-
79-
var ensureFunctionIsNotPassed = () =>
80-
log.error('td.object', `Functions are not valid arguments to \`td.object\` (as of [email protected]). Please use \`td.function()\` or \`td.constructor()\` instead for creating fake functions.`)

test/src/object-test.coffee

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ describe 'td.object', ->
1212
And -> @testDouble.now.toString() == '[test double for ".now"]'
1313
And -> @testDouble.otherThing == 8
1414

15+
describe.only 'creating an object that is an instance of a prototypal thing', ->
16+
Given -> @type = class Thing
17+
foo: -> 'bar'
18+
When -> try td.object(new @type()) catch e then @error = e
19+
Then -> expect(@error.message).to.contain('To create a fake object')
20+
1521
describe 'making a test double based on an array of strings', ->
1622
Given -> @testDouble = td.object(['biz','bam','boo'])
1723
When -> td.when(@testDouble.biz()).thenReturn('zing!')

0 commit comments

Comments
 (0)