Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用mocha测试Node.js api_Node.js_Api_Mocha.js_Expect.js - Fatal编程技术网

使用mocha测试Node.js api

使用mocha测试Node.js api,node.js,api,mocha.js,expect.js,Node.js,Api,Mocha.js,Expect.js,我正忙于为我编写的现有NodeJSAPI构建功能测试。我正在使用mocha和expect.js 一个失败的测试是当我想对url参数的存在性进行否定测试时。我想要的是,如果参数丢失,则向服务的使用者发送一条消息 目前我所做的阳性检测有效: var request = require('request'); it('get a specific user from the api', function (done) { request.get('http://localhost:8080/

我正忙于为我编写的现有NodeJSAPI构建功能测试。我正在使用mocha和expect.js

一个失败的测试是当我想对url参数的存在性进行否定测试时。我想要的是,如果参数丢失,则向服务的使用者发送一条消息

目前我所做的阳性检测有效:

var request = require('request');

it('get a specific user from the api', function (done) {
    request.get('http://localhost:8080/api/v1/user/123', function (error, response, body) { ... });
});
但是,以下操作不起作用,我得到的只是一个超时:

it('should return a 400 message if UserId is not supplied stating that "UserId expected"', function(done) {
    request.get('http://localhost:8080/api/v1/user/', function (error, response, body) { 
        if (!error && response.statusCode == 400) {
            expect(body).to.be.equal('UserId expected');
            done();
        }
    });
});
上述两项测试均测试此端点:

app.get('/api/v1/user/:userid', function (req, res) {
    if(!req.params.userid) {
        return res.status(400).json('UserId expected');
    }

    ... get the user and return it with status 200 ...
});
但是,如前所述,第一次测试成功,而第二次测试超时

更新1: 此外,我得到以下输出:

GET /api/v1/user/123 200 2.905 ms - 274
GET /api/v1/user/ - - ms - -
更新2: 因此,添加以下代码可以解决问题,但并不能真正解决问题:

app.get('/api/v1/user', function (req, res) {
    return res.status(400).json('UserId expected');
});
显然,我不希望有一段相当多余的代码。我很困惑,为什么我不能输入“api/v1/user/:userid”端点,而只是测试其中是否存在
:userid
?这就好像nodejs需要存在
:userid
,否则端点就不存在。这是正确的吗

我遗漏了什么?

根据done,回调必须始终被调用,如果出现错误也是如此。事实上,它接受了一个错误。 因此,我认为您必须用以下代码重写代码:

it('should return a 400 message if UserId is not supplied stating that "UserId expected"', function(done) {
    request.get('http://localhost:8080/api/v1/user/', function (error, response, body) {
        if (error) return done(error);
        if (response.statusCode != 400) return done(new Error("Not 400"));

        expect(body).to.be.equal('UserId expected');
        done();
    });
});

所以我做了更多的调查,并就这个问题问了另一个问题

最后的问题是我没有完全理解express框架。基本上,我在问题的更新2中提到的是我需要遵循的路由,并指定一个不需要参数的路由,但在这种情况下返回我期望的错误消息

这种方法的问题是,在某些情况下,可能需要在ur中使用多个参数。然而,这将意味着为满足每个未定义参数的场景而进行的排列变得有点令人生畏。(2个参数表示4种组合,3个参数表示9种组合等)这将是一场维护噩梦


从现在起,我的方法将遵循这样一种方法,即我想要传递给服务器的任何信息都将通过JSON发送到每个请求的主体中。这将允许我只使用一条路由。

如果服务器返回某些内容,您是否拒绝?你能不能在两个站点上都添加一些console.log/breakpoints来查看它从哪里得到stuckt?你怎么知道你的第一次测试成功了?没有断言,或者你只是把它砍掉了?我已经添加了控制台。服务器和测试中都有日志,我没有收到服务器的响应。我想请求一定是被服务器端卡住了,但我不知道在哪里,因为我在服务器端点的第一行有一个console.log,但它从未被击中。你说得对,我按照你描述的方式重写了它,而且我在request.get中的回调中添加了console.log。然而,我从未从那些console.log中得到结果,仍然只是超时。这让我相信有些事情我没有在服务器端处理,但我从来没有从我放在端点第一行的console.log中得到响应。。。。