问题
In an ember-cli app, testing is done using ember-qunit.
I would like to mock HTTP requests, but have not been able to find a recommended way of doing this in the documentation.
I have found this thread which discusses this, but it appears to be out of date (for ember-cli anyway).
How do you mock HTTP requests?
回答1:
This is how I mock HTTP requests. One improvement could be done by encapsulating mockjax
with helper like:
function stubEndpointForHttpRequest(url, json) {
$.mockjax({
url: url,
dataType: 'json',
responseText: json
});
}
So you can easily switch for another library like sinon
or whatever.
module('Integration - Signin Tests', {
setup: function(){
App = startApp();
},
teardown: function(){
Ember.run(App, 'destroy');
$.mockjaxClear(); // Don't forget to clear mockjax
}
});
test('Signin with valid data', function(){
expect(2);
stubEndpointForHttpRequest('api_url', 'response_json');
// Write your test
});
I Hope this helps
来源:https://stackoverflow.com/questions/24751690/mocking-http-requests-in-ember-qunit