Created
October 1, 2013 08:15
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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