Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use td.object with "callable objects" #232

Closed
Schoonology opened this issue Apr 24, 2017 · 4 comments
Closed

Cannot use td.object with "callable objects" #232

Schoonology opened this issue Apr 24, 2017 · 4 comments

Comments

@Schoonology
Copy link
Contributor

function SomeCallable() {}
SomeCallable.doInterestingThing = function () {}

module.exports = SomeCallable

This "callable object" pattern is common in the Express-centric world, and is not currently mockable with td.object. I understand the desire to Not Mock What We Don't Own, but I ran into this while trying to isolate a unit in client (i.e. Not Mine, But Not Practically Shimmable) code.

In lieu of an undiscussed PR, here's the patch for a sample test:

diff --git a/test/src/object-test.coffee b/test/src/object-test.coffee
index 8cf1896..0dd0318 100644
--- a/test/src/object-test.coffee
+++ b/test/src/object-test.coffee
@@ -24,6 +24,12 @@ describe 'td.object', ->
     Then -> expect(@result.message).to.contain(
       "Please use `td.function()` or `td.constructor()` instead")

+  describe 'passing a callback object to td.object', ->
+    Given -> @callable = ->
+    Given -> @callable.someMethod = ->
+    Given -> @testDouble = td.object(@callable)
+    When -> td.when(@testDouble.someMethod('hi')).thenReturn('consider me greeted')
+    Then -> @testDouble.someMethod('hi') == 'consider me greeted'

   if global.Proxy?
     describe 'creating a proxy object (ES2015; only supported in FF + Edge atm)', ->
@Schoonology
Copy link
Contributor Author

Do we want to try to support this? The way this pattern materializes, td.object(Object.keys(hopefully_just_this_but_sometimes.constructor.prototype)) is a workaround. It's obviously annoying.

@searls
Copy link
Member

searls commented Apr 24, 2017

Hey schoon, it may be a few days (Thursday) until I get to this. Is that ok?

@Schoonology
Copy link
Contributor Author

Totally fine. Just wanted to capture this before I have to move on.

@searls
Copy link
Member

searls commented Jun 18, 2017

Hey Schoon, I'm really sorry for taking two months to read this properly, but what you described right out the gate can already be done by passing SomeCallable to td.function(), as it'll also replace any first-layer function properties underneath it.

@searls searls closed this as completed Jun 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants