Mocking HTTP requests in ember-qunit

删除回忆录丶 提交于 2020-01-24 22:05:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!