question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Stubbing callbacks

See original GitHub issue

What if you could do this:

var fetch = td.function()

td.when(fetch('/some/path', td.callback(null, 'some data'))).thenReturn(undefined)

myFunction(fetch)

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:

myFunction(fetch) {
  fetch('/some/path', function(er, data){
    console.log('my data is', data)
  }
}

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:20 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
searlscommented, May 14, 2019

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 td.callback matcher would be passed to the callback function, since all its args are forwarded.

Would this work?

const cb = td.when(td.func(), {delay: 500}).thenCallback(null, 'some data')
td.when(someFunc('arg1', cb)).thenResolve('whatever')
0reactions
mbaranovskicommented, May 15, 2019

That’s what I was looking for, thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I stub callbacks of a method? - Stack Overflow
I am using Firebase Phone Auth in my Flutter project and want to test my auth class. I know how to use when()...
Read more >
Using Stub Callbacks - Parasoft C/C++test Professional 10.4.0 ...
Stub Callbacks provide a powerful mechanism for programming test case-specific stub logic. For each test case, you can define specific stub ...
Read more >
Testing Callbacks with Mockito - Baeldung
Now we'll look at a common solution for stubbing methods that have callbacks using Mockito's Answer object and doAnswer method to stub the ......
Read more >
Mockito 3 Stubbing with Callback - Junit 5 Jupiter | wesome.org
Callback with stub is a bit controversial feature hence originally was not included in Mockito. The callback method returns an Answer Interface.
Read more >
Stubs - Sinon.JS
Invokes callbacks passed as a property of an object to the stub. Like yield , yieldTo grabs the first matching argument, finds the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found