【问题标题】:protractor + aurelia: mock http量角器 + aurelia:模拟 http
【发布时间】:2016-06-28 04:27:42
【问题描述】:

我正在寻找一种使用量角器和 aurelia 模拟 HTTP(S) 请求的方法。我知道有量角器插件可以做到这一点,但它们似乎放在一个角度模块中,因为我没有使用角度那些似乎不是一个选项。

对于我们针对实时 API 的用例测试远非理想。我们要做的测试最好是基于一个固定的json mock

有人对如何做到这一点有建议吗?

【问题讨论】:

  • stackoverflow.com/a/35141543/725866 可能不是您想要的,但它展示了如何用模拟替换 http 客户端
  • 谢谢,但我以另一种方式解决了问题,我已经回答了我自己的问题......

标签: json http mocking protractor aurelia


【解决方案1】:

好的,我想我找到了一个不需要对应用程序代码进行任何更改的解决方案。它不是很漂亮,但它似乎工作:

/**
 * If you have a problem...
 * if no one else can help...
 * and if you can find them...
 * maybe you can hire...
 * fakeXHR.
 *
 * Overwrites XMLHttpRequest and auto resoles with status and response
 * @param {Number} status The statuscode to resolve with
 * @param {String} response, The response to resolve with
 */
function fakeXHR(status, response) {
    browser.executeScript(function (args) {
        var status = args[0];
        var response = args[1];

        function FakeXHR() {
            this.open = function() {}
            this.send = function() {}
            this.setRequestHeader = function() {}

            setTimeout(function() {
                this.response = response;
                this.status = status;
                this.onload();
            }.bind(this), 0);
        }

        XMLHttpRequest = FakeXHR;
    }, [status, response])
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-04
    • 1970-01-01
    • 2016-03-01
    • 2013-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多