javascript - faking xmlhttprequests with casperjs -


i'm writing end-to-end tests casperjs , fake ajax server responses

i've came idea of including simple script mocks xmlhttprequest object , return expected results, following

var ajax_requests = [ ['get', '/jobs', json.stringify(jobs)] ], stubs = stubs || {}; function setup_ajax(){ stubs.server = sinon.fakeserver.create(); _.each(ajax_requests, function(r){ //r[1] = "http://localhost:8000" + r[1] r[2] = [200, { "content-type": "application/json" }, r[2]] stubs.server.respondwith.apply(stubs.server, r) }) stubs.server.autorespond = true; stubs.server.autorespondafter = 2; } 

then call setup_ajax in casper test like

casper.then(function(){ this.evaluate(setup_ajax) } 

but seemingly future ajax requests still avoid xmlhttprequest implementation.

i've tried running setup_ajax on fly, using $.ready() , having called casper too, neither of these worked

more interestingly, checking objects' existence strangely fails.

function setup_ajax(){ return typeof(sinon) } casper.then(function(){ var x = this.evaluate(setup_ajax) casper.log(x) // logs 'null' } 

but sinon included, @ least casper did not cause errors when i've made calls outside setup_ajax function, caused error when deliberately had sinon excluded.

do have ideas on mocking xmlhttprequests under casperjs?

you can use sinon.js fake xmlhttprequest.


Comments

Popular posts from this blog

JQuery Autocomplete without using label, value, id -

c++ - Accessing inactive union member and undefined behavior? -

JAVA - what is the difference between void and boolean methods? -