Mocking HTTP requests in ember-qunit

2019-08-13 14:29发布

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条回答
劫难
2楼-- · 2019-08-13 14:56

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

查看更多
登录 后发表回答