在ember-qunit中模拟HTTP请求

时间:2014-07-15 06:51:58

标签: ember.js mocking qunit ember-cli

在ember-cli应用程序中,使用ember-qunit进行测试。

我想模拟HTTP请求,但无法找到推荐的方法in the documentation

我有found this thread讨论过这个问题,但它看起来已经过时了(无论如何都是ember-cli)。

如何模拟HTTP请求?

1 个答案:

答案 0 :(得分:3)

这是我模拟HTTP请求的方式。可以通过使用帮助器封装mockjax来完成一项改进:

function stubEndpointForHttpRequest(url, json) {
    $.mockjax({
        url: url,
        dataType: 'json',
        responseText: json
    });
}

因此,您可以轻松切换其他库,例如sinon或其他任何库。

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
});

我希望这有帮助