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

Shortcut for new stub returning args #187

Closed
davidbarton opened this issue Feb 14, 2017 · 9 comments
Closed

Shortcut for new stub returning args #187

davidbarton opened this issue Feb 14, 2017 · 9 comments

Comments

@davidbarton
Copy link

When I use one line to create new function stub and return some arguments, it gets messy really fast. So instead of writing

const t1 = td.when(td.function()(options, limit)).thenReturn('first', 'last')

It would be possible to write something like this

const t1 = td.whenFn(options, limit).thenReturn('first', 'last')
@searls
Copy link
Member

searls commented Feb 14, 2017

I don't really understand this proposed API. How would you specify args and matchers in the stubbing?

@davidbarton
Copy link
Author

Arguments and matches are put directly to new shortcut fn. In this case options, limit are arguments.

  • no argument stub const t1 = td.whenFn().thenReturn('first', 'last')
  • argument stub const t1 = td.whenFn(options, limit).thenReturn('first', 'last')
  • matcher stub const t1 = td.whenFn(td.matchers.isA(Number)).thenReturn('first', 'last')

@searls
Copy link
Member

searls commented Feb 14, 2017

I don't understand, how would an implementation of whenFn know the difference between when it's being passed an argument and when it's being passed an options object?

@searls
Copy link
Member

searls commented Feb 14, 2017

(btw, limit shouldn't be necessary, because times is an existing options entry) (Doc)

@davidbarton
Copy link
Author

Ohh sorry for that options and limit are just confusing variable names. They are meant to be any arguments. I did copy them from my test.

@searls
Copy link
Member

searls commented Feb 14, 2017

Hmm, ok. So that yields the next question: how do people set options on the stubbing?

@davidbarton
Copy link
Author

I did not think of them, would probably let you use full format when you need options and have shortcut only without them.

@searls
Copy link
Member

searls commented Feb 14, 2017

Ok, I understand the proposal now.

I'm pretty conservative in keeping the surface area of the test double API distinct. Since it's an opinionated library, I generally only want there to be one way to do something unless there's a tremendous advantage to providing a shortcut. Because the variability in stubbing & verifying is, frankly, enormous, any synonyms or shortcuts risk confusing people even more as they try to learn an already confusing API.

I am especially reticent to add new top-level methods to the td object, since it increases the cost of someone exploring the API top-down to understand what the library does.

I don't think this proposed API adds enough benefit to outweigh those documentation and confusion risks. When I look at Sinon, with its several ways to create the same stubbing, the numerous bugs they encounter when an option works in one stubbing style but not another is a real problem that I am careful to avoid here.

I'd recommend defining this helper yourself with something like:

whenFn = function (args...) {
  return function (stubbings...) {
    return td.when(td.func()(...args)).thenReturn(...stubbings)
  }
}

@searls searls closed this as completed Feb 14, 2017
@davidbarton
Copy link
Author

Ok, thanks for consideration. I will probably do own helper as you suggested since it makes my code lot more readable.

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