【问题标题】:expect (js) not working inside superagent (js)期望(js)在超级代理(js)中不起作用
【发布时间】:2016-02-11 09:06:50
【问题描述】:

我正在尝试为我一直在创建的 Rest API 执行 TDD。 NodeJS 新手。

我创建了一个 Rest API,并在响应中执行所有 expect 检查。为了发出 HTTP 请求,我使用 SuperagentJS(也尝试过 RequestJS)。

这是我的代码的样子(仅片段,不是完整代码)

var expect = require("chai").expect;
var request = require("superagent");

describe("Creation of New Entity", function(){
    it("Create a New Entity", function(){
        request
            .get("http://localhost")
            .end(function(err, httpResponse){
                expect("1234").to.have.length(3);//equals(500);
                expect(200).to.equals(200);
        });
    });
});

无论我尝试什么,mocha 总是能成功。 (所有测试用例均通过)

请告诉我在这里缺少什么。我应该怎么做才能在httpRespnse 上实现测试用例。我确信该请求工作正常,因为每当我使用 console.log(httpResponse.text) 时,它都会返回默认的 apache 主页。

【问题讨论】:

    标签: node.js superagent expect.js


    【解决方案1】:

    node.js 中的所有网络都是异步的,因此您必须使用 it("Create a New Entity", function(done) { 的 mocha 异步风格,并在测试完成时调用 done 回调。

    var expect = require("chai").expect;
    var request = require("superagent");
    
    describe("Creation of New Entity", function(){
        it("Create a New Entity", function(done){
            request
                .get("http://localhost")
                .end(function(err, httpResponse){
                    expect(err).not.to.exist();
                    expect("1234").to.have.length(3);//equals(500);
                    expect(200).to.equals(200);
                    done()
            });
        });
    });
    

    【讨论】:

    猜你喜欢
    • 2014-10-08
    • 2018-02-12
    • 1970-01-01
    • 1970-01-01
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    • 2021-07-30
    • 2018-09-30
    相关资源
    最近更新 更多