-
-
Save nathanpalmer/11258730 to your computer and use it in GitHub Desktop.
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
var httpResponseQueue = Ember.A(); | |
var checkQueueInterval = null; | |
var checkQueue = function() { | |
for (var i = 0; i < httpResponseQueue.length; i++) { | |
var queuedItem = httpResponseQueue[i]; | |
var found = fakehr.match(queuedItem.verb.toUpperCase(), queuedItem.url); | |
if (found) { | |
found.respond(queuedItem.status || 200, {'content-type': 'application/json'}, queuedItem.body); | |
} | |
} | |
}; | |
var queueHttpResponse = function(app, verb, url, body, status) { | |
if(typeof body !== 'string') { body = JSON.stringify(body); } | |
var newItem = { verb: verb, url: url, body: body, status: status }; | |
var existingQueuedItem = httpResponseQueue.find(function(item) { | |
return item.verb === newItem.verb && item.url === newItem.url; | |
}); | |
if (existingQueuedItem) { | |
httpResponseQueue.removeObject(existingQueuedItem); | |
} | |
httpResponseQueue.push(newItem); | |
if (!checkQueueInterval) { | |
// we need to do this outside of the ember run loop or tests will get stuck anyway | |
checkQueueInterval = window.setInterval(checkQueue, 10); | |
} | |
}; | |
var clearHttpResponseQueue = function() { | |
httpResponseQueue = Ember.A(); | |
window.clearInterval(checkQueueInterval); | |
checkQueueInterval = null; | |
}; | |
Ember.Test.registerHelper('queueHttpResponse', queueHttpResponse); | |
Ember.Test.registerHelper('clearHttpResponseQueue', clearHttpResponseQueue); |
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
module('my tests', function() { | |
setup: function() { | |
fakehr.start(); | |
queueHttpResponse('GET', '/1', { something: 1 }); | |
queueHttpResponse('GET', '/2', { something: 2 }); | |
}, | |
teardown: function() { | |
fakehr.reset(); | |
clearHttpResponseQueue(); | |
} | |
}); | |
test('Test1', function() { | |
// overriding one response for this test only | |
queueHttpResponse('GET', '/1', { something: "1.1" }); | |
visit('/home'); | |
andThen(function() { | |
// your asserts here | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Changed