0

I am trying to use Lab to test some code. However, for some reason when I put the right payload my code does nothing. The promise doesn't seem to get returned:

'use strict';

const Server = require('../server');

const options = {
    method: 'POST',
    url: '/api/users',
    payload: JSON.stringify({ mobile: '3342329224' })
};

Server.inject(options, (response) => {

    if (response) {
        console.log(response.payload);
    }
    else {
        console.log('Nada');
    }
});

If I edit the code so that there is no payload or it doesn't match my Joi validation then it I get response:

{"statusCode":400,"error":"Bad Request","message":"child \"mobile\" fails because [\"mobile\" must be larger than or equal to 10]","validation":{"source":"payload","keys":["mobile"]}}

module.exports = {
    method: 'POST',
    path: '/api/users',
    config: {
        auth: false,
        handler: (request, reply) => {

            //looks up payload in db otherwise creates entry
            User.findOne({
                mobile: request.payload.mobile
            }, (err, user) => {

                if (err) {
                    throw err;
                }
                if (user) {
                    // uses twillio to send code
                    sendVerificationText(user, (err, result) => {

                        if (err){
                            throw err;
                        }
                        if (result === true) {
                            // this is what I expect to happen when testing
                            reply('code sent').code(201);
                        }
                        else {
                            throw Boom.badRequest(err);
                        }
                    });
                }
                else {
                    // the user should exist so....
                    const user = new User();
                    user.mobile = request.payload.mobile;
                    user.admin = false;
                    user.save((err, user) => {

                        if (err) {
                            throw Boom.badRequest(err);
                        }
                        sendVerificationText(user, (err, result) => {

                            if (err){
                                throw err;
                            }
                            if (result === true) {
                                reply('code sent').code(201);
                            }
                            else {
                                throw Boom.badRequest(err);
                            }
                        });
                    });
                }
            });
        },
        // Validate the payload against the Joi schema
        validate: {
            payload: createUserSchema
        }
    }
};

I should mention that this code works when I run the server and test the api by hand. I can't figure it out.

4

1 回答 1

0

很抱歉原来的答案没有得到任何地方。我已经回去尝试使用实验室重新创建您的测试: https ://github.com/davethomas11/stackoverlfow_Q_39432656/blob/master/test/users.js

它似乎不适合我。我收到了预期的响应代码。根据实验室的文档,我使用代码库(新依赖项)在函数中添加了几个断言:

lab.test("user post", function (done) {

    const options = {
        method: 'POST',
        url: '/api/users',
        payload: JSON.stringify({ mobile: '3342329224' })
    };

    Server.inject(options, (response) => {

        if (response) {
            console.log(response.payload);
        }
        else {
            console.log('Nada');
        }

        Code.expect(response.statusCode).to.equal(201);
        Code.expect(response.payload).to.equal("code sent");
        done();
    });
});

好奇你在运行测试时看到了什么输出?命令行是否只为您挂起?检查我的 github 项目,了解如何为我实施测试以发现差异。希望这对您有所帮助。

哦..你知道你需要什么来确保你在你的测试中调用 done() 来结束它;)可能为什么它会挂在你身上

供参考的原始答案: 我已尽我所能重新创建了您的环境。不得不说我喜欢这个 hapi.js 框架。非常好!快速启动服务器的方法。竖起大拇指。不错的选择。

所以这是我根据您发布的代码通过模拟发现的: https ://github.com/davethomas11/stackoverlfow_Q_39432656

在该解决方案中,您将找到设置验证的位置:

validate: {
            payload: createUserSchema
        }

我假设createUserSceme是您设置为有效载荷值的函数。如果它是一个函数,它将导致请求挂起并且永远不会返回!因此,您看到的行为就好像永远不会返回承诺一样。服务器从不响应。如果我把它改成这个......

validate: {
            payload: createUserSchema()
        }

然后我们都没事,验证可以继续。所以这里的目标是确保 validate 的值是一个对象。

请注意,我通过猜测嘲笑了您的 createUserSchema。因此,如果您想深入了解下一个问题。请发布更多代码,我很乐意尝试看看我是否可以提供更多帮助=)(继续观看 Nacros 第 2 季的更多内容,我明天再回来查看)

于 2016-09-11T05:05:09.547 回答