This is my js file:
My.File = {
someMethod: function() {
var finalURL = "https://some.url.com"
var somePostCall = $.post(My.URL, {
sessionToken: My.Session.sessionToken
});
somePostCall.done(function(response) {
//do something with the response ===> I want to test this function, and mock the 'response'
});
}
}
What I'm trying to do is to write a test which covers the function being passed in the "done" call. I don't want to make the actual ajax call, I want to moke the response from the http call, and still want to have the somePostCall promise to being invoked. And when the "done" method, the 'response' has to be mocked with something made up by me.
I've been trying to capture the response from the $.post call. Like this:
spyOn($, 'post').and.callThrough();
My.File.someMethod();
var promise = $.post.calls.mostRecent().args[0];
The problem is that .args[0] doesn't return the value from $.post, it is just the params being passed. If I can spy the response from $.post, then I can spy it, and capture the function being passed to the 'done' method. So that way I can at least call it manually, and pass a mocked 'response'.
Any ideas?
Aucun commentaire:
Enregistrer un commentaire