在ember-qunit中模拟HTTP请求 [英] Mocking HTTP requests in ember-qunit
问题描述
在ember-cli应用中,测试是使用ember-qunit完成的.
In an ember-cli app, testing is done using ember-qunit.
我想模拟HTTP请求,但无法找到推荐的方法来执行此操作在文档中.
I would like to mock HTTP requests, but have not been able to find a recommended way of doing this in the documentation.
我有找到了该线程对此进行了讨论,但似乎已经过时了(无论如何,对于ember-cli).
I have found this thread which discusses this, but it appears to be out of date (for ember-cli anyway).
您如何模拟HTTP请求?
How do you mock HTTP requests?
推荐答案
这是我模拟HTTP请求的方式.可以通过使用诸如以下的辅助程序封装mockjax
来完成一项改进:
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
});
}
因此,您可以轻松地切换到另一个库,例如sinon
或其他.
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
});
我希望这会有所帮助
这篇关于在ember-qunit中模拟HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!