Testing events in XMLHttpRequest ... what am I doing wrong?

1,510 views
Skip to first unread message

Matěj Cepl

unread,
Feb 23, 2013, 5:11:52 PM2/23/13
to jasmi...@googlegroups.com
Hi,

I am trying to test for the bad path of my XMLHttpRequest (plain
JavaScript one, not any library) processing function with jasmine, and
the best I can think about is this:

describe("LoadFeed", function() {
beforeEach(function() {
spyOn(XMLHttpRequest.prototype, 'open').andCallThrough();
spyOn(XMLHttpRequest.prototype, 'send');
});

// ...

it("should fail when XMLHttpRequest fails", function() {
spyOn(XMLHttpRequest.prototype, 'onerror').andCallThrough();
spyOn(XMLHttpRequest.prototype,
'onload').andCallFake(function(evt) {
var new_event = document.createEvent("ProgressEvents");
new_event.initProgressEvent("error", evt.canBubble,
evt.cancelable, evt.lengthComputable, evt.loaded,
evt.lengthComputable);
evt.target.dispatchEvent(new_event);

podcast.load_feed(URL_FEED);
expect(XMLHttpRequest.prototype.onerror).toHaveBeenCalled();
});
});

However, when I try to run this I get this error:

TypeError: Value does not implement interface
XMLHttpRequestEventTarget. in
file:///home/matej/projekty/elasmotherium/wsgi\
/static/spec/lib/jasmine.js (line 2415)

Any idea, how to do it right?

Thanks for any response,

Matěj

--
http://www.ceplovi.cz/matej/, Jabber: mcepl<at>ceplovi.cz
GPG Finger: 89EF 4BC6 288A BF43 1BAB 25C3 E09F EF25 D964 84AC

In those days spirits were brave, the stakes were high, men were
real men, women were real women and small furry creatures from
Alpha Centauri were real small furry creatures from Alpha
Centauri.
-- Douglas Adams
signature.asc

Davis W. Frank

unread,
Feb 23, 2013, 5:16:04 PM2/23/13
to jasmi...@googlegroups.com
XMLHttpRequest is a bit of a special beast.

Check out the jasmine-ajax add-on: http://github.com/pivotal/jasmine-ajax for a better fake for HTTP.

--dwf
--
thx,
--dwf

Ben Loveridge

unread,
Feb 23, 2013, 6:36:49 PM2/23/13
to jasmi...@googlegroups.com
If you are not using jQuery, another tool that works very well is sinon.js. It functions at a level lower than libraries and lets you easily provide fabricated responses for any combination of URLs, methods, etc.
-- 
Ben Loveridge 
--
You received this message because you are subscribed to the Google Groups "Jasmine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jasmine-js+...@googlegroups.com.
To post to this group, send email to jasmi...@googlegroups.com.
Visit this group at http://groups.google.com/group/jasmine-js?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

JR Boyens

unread,
Feb 23, 2013, 6:51:50 PM2/23/13
to jasmi...@googlegroups.com

Version 2.0 of jasmine-ajax supports mocking XMLHttpRequest directly and removes the jQuery requirement.

2.0 has some features that are not yet complete, but mocking should work just fine. Find it in the 2_0 branch of jasmine-ajax.

Reply all
Reply to author
Forward
0 new messages