Skip to content

Instantly share code, notes, and snippets.

@op1ekun
Created October 1, 2013 08:15
Show Gist options
  • Save op1ekun/6775312 to your computer and use it in GitHub Desktop.
Save op1ekun/6775312 to your computer and use it in GitHub Desktop.
How to easily mock ajax calls using Jasmine's spies. Binds on success callback to the custom object to simulate get('responseData') method.
it('test AJAX mock', function() {
spyOn(Y.io, 'request')
.andCallFake(function(url, config) {
console.log('fake ajax', arguments);
var bound = config.on.success.bind({
'get' : function(name) {
if (name === 'responseData') {
return {
'some' : {
'fake' : {
'response' : {
}
}
}
};
}
}
});
bound();
});
Y.io.request('http://localhost:1248', {
data : 'json',
method : 'post',
on : {
success : function() {
console.log('success', this.get('responseData'));
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment