【问题标题】:How to test an external HTTP resource using mocha and requestify如何使用 mocha 和 requestify 测试外部 HTTP 资源
【发布时间】:2014-07-25 13:06:42
【问题描述】:

我正在尝试使用 mocha 对外部 HTTP 资源进行简单测试。这是我的代码:

describe('bing.com', function() {
    it('should return 200 for a GET request', function() {
        var requestify = require('requestify');

        requestify.get('http://bing.com')
            .then(function(response) {
                // Get the response body (JSON parsed or jQuery object for XMLs)
                console.log(response.getBody());
                done();
            });
    });
});

测试只是说通过了,但我的console.log 呼叫从未显示。 mocha 是否在收到 http 响应之前完成?

【问题讨论】:

    标签: node.js mocha.js requestify


    【解决方案1】:

    您没有向您的测试函数提供 done 回调:

    describe('bing.com', function() { it('should return 200 for a GET request', function(done) { ...

    要捕获此类错误,您应该使用 JSHint(或 Jslint)检查您的代码。两者都会通知您,由于未定义变量,您的 done() 调用将不起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-29
      • 1970-01-01
      • 1970-01-01
      • 2011-02-28
      • 2020-03-06
      • 1970-01-01
      • 2012-08-28
      • 2021-07-17
      相关资源
      最近更新 更多