Sinon.js: Need help regarding correct way to stub and analyze the return

153 views
Skip to first unread message

Prateek Mehrotra

unread,
May 15, 2013, 9:24:07 PM5/15/13
to sin...@googlegroups.com

I am writing a node app using coffeescript and doing some TDD using mocha.js. Please can someone point me correct way to write the test.

What I want to do: Fake response of 'ra.do_get'and then call method 'all_tests' to analyze return value

var ra = require('./request_adapter');

class QueryHandler
  constructor: (@adapter) ->
    @klass  = "QueryHandler"
    @url    = "http://localhost:#{port[@adapter]}/"
    logger.debug "[#{@klass}] creating .."

  all_tests: (cb) ->
    logger.info "#{@adapter}: requesting all test - #{adapter_apis.ALL_TESTS_SESSION}"
    //RequestAdapter is another class doing some GET/POST etc (using request NPM)
    ra   = new RequestAdapter(@url)
    _url = @url + adapter_apis.ALL_TESTS_SESSION

    ra.do_get _url, (error , response) ->
      if error
        return cb({error: true, message: "#{@adapter} adapter errored out"})
      else
        return cb(null,response)
    return   

Thanks

Erik Trom

unread,
May 15, 2013, 9:37:00 PM5/15/13
to sin...@googlegroups.com
While I can't really provide the answer in full, I do know where to find it as I just came across it while learning some things. 

Chapter 14 of Test Driven Javascript Development by Christian Johansen has the answer your looking for and the refactor you'll likely want to aim towards -- promises… 

Hope that helps -- and it will if you read that chapter, swear on it… 

It doesn't use mocha, but when I drove the examples out, I chose to use mocha and it worked out well.. 

--Erik


--
You received this message because you are subscribed to the Google Groups "Sinon.JS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sinonjs+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Prateek Mehrotra

unread,
May 15, 2013, 9:42:44 PM5/15/13
to sin...@googlegroups.com
Thanks a lot for quick response. Hope I had the book :(

Erik Trom

unread,
May 15, 2013, 9:47:06 PM5/15/13
to sin...@googlegroups.com
You could try just looking at the code:


Good luck. It depends how new you are to the language, I was brand new and without the book, it would have been like 'huh'… But you may have the skillz already… 

--Erik

Erik Trom

unread,
May 15, 2013, 9:49:47 PM5/15/13
to sin...@googlegroups.com
I think the code is in the refactored state using promises, FYI, so take a look at the Q library by Chris Kowal to see how to wrap a regular node callback in a promise and it may help fill in the gaps


--Erik

Prateek Mehrotra

unread,
May 15, 2013, 9:52:00 PM5/15/13
to sin...@googlegroups.com
Thanks Erik for pointers. This is helpful !

Erik Trom

unread,
May 15, 2013, 9:56:22 PM5/15/13
to sin...@googlegroups.com
No prob. I struggled with the whole thing for a bit so I know your pain. One more thing that helped me was this episode of JavaScript Jabber which focuses on promises, which may soon be apart of the language and basically remove callback hell, it's worth listening too, even if it's just for fun… (Guests on the episode are the authors of the Q library, they explain it well)


At the end of the episode, they talk about testing async code with promises and mocha, FYI

Cheers

--Erik
Reply all
Reply to author
Forward
0 new messages