Skip to content

Instantly share code, notes, and snippets.

@joefiorini
Created February 21, 2014 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joefiorini/9128085 to your computer and use it in GitHub Desktop.
Save joefiorini/9128085 to your computer and use it in GitHub Desktop.
Ember.Test.registerHelper('httpRespond', function(app, verb, url, body, status) {
function tryResponding() {
console.log("matching against url: ", url);
var found = fakehr.match(verb.toUpperCase(), url)
if (found) {
Ember.run(function() {
found.respond(status || 200, {'content-type': 'application/json'}, body);
});
}
return found;
}
if(typeof body !== 'string'){ body = JSON.stringify(body); }
if(!tryResponding()) {
fakehr.requestStarted(function(xhr) {
var success = tryResponding();
if (!success) {
throw new Ember.Error("No request intercepted for " + verb.toUpperCase() + " " + url + ". Intercepted requests were: " + fakehr.requests.map(function(r){ return r.method + " " + r.url}).join(", "));
}
});
}
});
/*
This is mildly crazy. Ember.Test increments requets on ajax start
so promises won't resolve until all xhrs complete, but were mocking
them all, so we just remove the ajaxStart and ajaxStop callbacks
until we can merge into master and put some of this behind flags.
*/
Ember.Test.onInjectHelpers(function() {
Ember.$(document).unbind("ajaxStart ajaxStop");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment