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

Inifinite recursion problem #231

Closed
tiagolr opened this issue Apr 23, 2017 · 7 comments
Closed

Inifinite recursion problem #231

tiagolr opened this issue Apr 23, 2017 · 7 comments

Comments

@tiagolr
Copy link

tiagolr commented Apr 23, 2017

I'm stubbing a method that is only called after a promise chain, to complete the test I must wait for that method to be called asynchronously, but when I start verifying the results I get stuck in an infinite loop.

Easier to understand with a code example:

it ('replies something', (done) => {
  const reply = td.function()
  obj.reply = reply

  td.when(reply(), {ignoreExtraArgs: true}).thenDo(() => {
    td.verify(reply(td.matchers.anything), { ignoreExtraArgs: true }) // triggers infinite loop
    done() // code never reached
  })

 obj.fetchResults()
}

I am fairly new to this framework, is there anything I should be doing instead? Or is this a limitation?

@searls
Copy link
Member

searls commented Apr 23, 2017

I don't understand what you're trying to accomplish with the above configuration. Rather than critique a lot about what I can see in the above example with testing craft/theory, could you provide a minimal example function so I can show how I would test it?

@searls
Copy link
Member

searls commented Apr 23, 2017

(Closing for now, because using andDo to td.verify a double from its own stubbing isn't a supported usage and I don't think we need to consider it one. I may reopen)

@searls searls closed this as completed Apr 23, 2017
@tiagolr
Copy link
Author

tiagolr commented Apr 24, 2017

I think this can be a normal use case, waiting for a stub to be called to verifiy and finish a test, since the stub is at the end of a promise chain, there's a need to wait for it.

Anyway found a work around:

setTimeout(() => {
   td.verify(reply)
   done()
})

This way the verification is only done in the nextTick after the promises are executed (note: does not work using process.nextTick)

@searls
Copy link
Member

searls commented Apr 24, 2017

I mean to say you should never need to verify that which you have also stubbed. It has, by definition, already been called.

Can you provide an example of code needed to be tested?

@tiagolr
Copy link
Author

tiagolr commented Apr 24, 2017

What I'm doing is verifying the response to commands of a microservice (slack bot)

Its not integration tests, but really makes the tests much more readable and meaningful by verifying the bot responses to commands.

Example in pseudo code:

td.object(bot.api1)
td.object(bot.api2)

bot.reply = td.function()

it ('responds api data from "get api services"')
   td.when(bot.api1.get()).thenResolve('ok')
   td.when(bot.api2.get()).thenResolve('ok')

   bot.eval("get api services")
   td.verify(bot.reply(td.matchers.contains(/ok ok/i))

hope this make sense, anyway I'm now able to wait for the bot reply at the end of promise chains to by using setTimeout so all is good

@searls
Copy link
Member

searls commented Apr 24, 2017

I hate to ask for a third time but can you please send an example of the code being tested if you want help

@tiagolr
Copy link
Author

tiagolr commented Apr 25, 2017

thanks Searls, not necessary, I'll create a gist if get stuck in a related issue in the future

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