-
Notifications
You must be signed in to change notification settings - Fork 142
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
Stubbing callbacks #66
Comments
Related: this introduces a situation where |
In this example, is there some assumed wiring-up happening so that |
@BaseCase yes i was just hand-waving - fixed |
or
|
I suppose alternatively you could just support the omission of a |
Jasmine spies default to doing nothing and returning nothing (i.e. You will lose this previously valid pattern: var fn = td.function();
var stub = fn.when();
fn({ “foo” : “bar” });
stub.thenReturn(“baz”); …but is anyone using it like that? |
Given that the API tries to be pretty English sentencey, maybe something like |
Sometimes (in rare cases) code relies on not having callbacks fire in-thread to work correctly. You might also want |
Also 👍 to @davemo's |
I think the So perhaps: td.when(fetch('/some/path', td.callback)).thenCallback(null, 'with data') This way the "how is the stubbing invoked" is still separate from "how does the stubbing behave". (I'm not sure what purpose or value there is in having Open to options with streamlined errorBacks. (keeping the node interface would be consistent, but ugly for the 99% case that the callback is a success callback) td.when(fetch('/some/path', td.callback)).thenCallback('with data') // same as cb(null, 'with data')
td.when(fetch('/some/path', td.callback)).thenErrorback('with err') // same as cb('with err') |
FWIW I find @jasonkarns syntax clearest, because of the separation he mentioned. |
^ @davemo, that is my inclination. From a user's perspective that would be least surprising i think |
@jasonkarns @deanius -- I started with what Jason suggested, but I dislike it for two reasons:
|
Why would
I'm conflicted on |
@jasonkarns after seeing the complete mess Sinon's chainable API became, In the example you gave I'd much rather just treat On Sat, Feb 13, 2016 at 2:51 PM Jason Karns notifications@github.com
|
Aside from just wanting things to be terser (and avoiding a special marker If someone has a On Sat, Feb 13, 2016 at 5:47 PM Justin Searls searls@gmail.com wrote:
|
Hey, a quick update: I implemented this on the These should all work (td.callback as a matcher w/ thenReturn): td.when(fetch('/some/path', td.callback(null, 'some data'))).thenReturn(undefined) And this (td.callback as position marker): td.when(fetch('/some/path', td.callback)).thenCallback(null, 'with data') And also this (implicit td.callback tacked on by the library as last argument when one isn't specified): td.when(fetch('/some/path')).thenCallback(null, 'with data') |
Hi @searls, is there any way force delay on td.callback only? I have an example like:
I'd like Thanks [EDIT]
|
This is already already supported, but if you want to control the callback function then you should configure it separately. Trying to do both in one shot would be too confusing, and any additional parameters to the Would this work? const cb = td.when(td.func(), {delay: 500}).thenCallback(null, 'some data')
td.when(someFunc('arg1', cb)).thenResolve('whatever') |
That's what I was looking for, thanks! |
What if you could do this:
The
td.callback
API would just immediately invoke whatever function it captures at call-time with the provided arguments.All so that you could synchronously specify this behavior in your source:
The text was updated successfully, but these errors were encountered: