Re: [jasmine-js] How to test a Deferred?

886 views
Skip to first unread message

Davis W. Frank

unread,
Sep 16, 2012, 9:17:42 PM9/16/12
to jasmi...@googlegroups.com
If you're dealing strictly with a timeout, you can use the MockClock. See http://pivotal.github.com/jasmine for simple examples.

you'll probably want to pass in a spy to your done call in order to verify that it does get called.

--dwf

On Sun, Sep 16, 2012 at 5:04 PM, jspeaks <jaye....@gmail.com> wrote:
I am looking for a terse example of how to test a Deferred with Jasmine.  I basically want to validate the done result.  A clear example would be appreciated.

>> Neighborhood.js <<
Neighborhood = function(){
  var me = {};

  me.updates = function(){
    var def = $.Deferred();

    setTimeout(def.resolve([{"aaa",200},{"bbb",300}]), 1000*5);

    return def.promise();
  };

  return me;
}

>> App.js <<
$(function(){
  var neighborhood = Neighborhood();
  neighorhood.updates().done(function(data){ console.log(data) });
})

I reviewed all the asynchronous examples, but I still don't have a clear understanding of how to test the result of a deferred in a terse manner.

Thanks in advance for an example,

Jaye Speaks
@jspeaks

--
You received this message because you are subscribed to the Google Groups "Jasmine" group.
To view this discussion on the web visit https://groups.google.com/d/msg/jasmine-js/-/v9PZHXyBAJoJ.
To post to this group, send email to jasmi...@googlegroups.com.
To unsubscribe from this group, send email to jasmine-js+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/jasmine-js?hl=en.



--
thx,
--dwf

Derick Bailey

unread,
Sep 16, 2012, 10:20:31 PM9/16/12
to jasmi...@googlegroups.com

The use of "setTimeout" vs a deferred makes no difference in how the test looks.

You can see an example of my own tests against deferreds with Jasmine and my Jasmine.Async add-on, here: https://github.com/derickbailey/bbclonemail/blob/master/spec/javascripts/mail.spec.js

I also have a post that shows more about unit testing async code, here: http://lostechies.com/derickbailey/2012/08/17/asynchronous-unit-tests-with-mocha-promises-and-winjs/ - this deals with Mocha, but with my Jasmine.Async add-on, the test implementation caries over almost perfectly.

Hope that helps.

  -- Derick

Derick Bailey
@derickbailey 
http://derickbailey.lostechies.com

Reply all
Reply to author
Forward
0 new messages