【问题标题】:Mocha test failing when sending invalid data to the server向服务器发送无效数据时 Mocha 测试失败
【发布时间】:2018-02-25 21:45:14
【问题描述】:

我正在使用 nodejs 和 mongodb 开发一个小型 todos 应用程序。

我这里有模型定义:

const Todo = new Schema({
    text: {
        type: String,
        require: true,
        minlength: 5,
        trim: true
    },
    completed: {
        type: Boolean
    },
    createdAt: {
        type: {type: Date, default: Date.now}
    }
});

如您所见, text 属性是必需的,如果它在读取请求时丢失,它应该会抛出错误。

在这里,我将数据发送到我的端点:

app.post('/todos', (req, res) => {
    let todo = new Todo({
        text: req.body.text,
        completed: req.body.completed
    });

    todo.save()
        .then((document) => {
            res.send(document);
        }, (error) => {
            res.status(400).send(error);
        })
});

最后,这是我对用户向服务器发送空数据集的特定场景的测试:

it('Should not create todo document with invalid body data', (done) => {
        request(app)
            .post('/todos')
            .send({})
            .expect(400)
            .end((error, res) => {
                if(error){
                    return done(error);
                }
                Todo.find()
                    .then((todos) => {
                        expect(todos.length).toBe(0);
                        done();
                    }).catch((error) => done(error));
            });
    });

运行测试后,由于某种原因,它抛出以下内容:

1) POST /todos
     Should not create todo document with invalid body data:
     Error: expected 400 "Bad Request", got 200 "OK"
      at Test._assertStatus (node_modules\supertest\lib\test.js:266:12)
      at Test._assertFunction (node_modules\supertest\lib\test.js:281:11)
      at Test.assert (node_modules\supertest\lib\test.js:171:18)
      at Server.assert (node_modules\supertest\lib\test.js:131:12)
      at emitCloseNT (net.js:1689:8)
      at process._tickCallback (internal/process/next_tick.js:152:19)

过去一个小时我一直在尝试调试它,但我找不到它有什么问题。谁能帮帮我?

更新

其他测试:

it('Should create a new todo', (done) => {
        let text = 'This is a string';

        request(app)
            .post('/todos')
            .send({text})
            .expect(200)
            .expect((res) => {
                let testString = res.body.text;
                expect(testString).toBe(text);
                expect(typeof testString).toBe('string');
                expect(testString.length).not.toBe(0);
            })
            .end((error, res) => {
                if(error) {
                    return done(error);
                }
                Todo.find()
                    .then((todos) => {
                        expect(todos.length).toBe(1);
                        expect(todos[0].text).toBe(text);
                        done();
                    }).catch((error) => done(error));
            });
    });

【问题讨论】:

    标签: node.js mocha.js jestjs supertest


    【解决方案1】:

    您应该在使用之前检查 text 和 completed 是否存在:

    app.post('/todos', (req, res) => {
        let text = req.body.text;
        let completed = req.body.completed;
        if(!completed) { completed = false; }
        if(!text) {
            res.status(400).send("Request parameters missing");
        } else {
            let todo = new Todo({
                text: req.body.text,
                completed: req.body.completed
            });
    
            todo.save()
                .then((document) => {
                    res.send(document);
                }, (error) => {
                    res.status(400).send(error);
                })
        }
    });
    

    同样在您的架构中,它应该是“必需”而不是“要求”

    【讨论】:

    • 另一个测试是什么?
    • 用其他测试更新了我的问题。
    • 这是您要使用的逻辑问题。我将以更新我的代码为例。
    • 您能详细说明一下吗?我正在学习,我希望了解它为什么不起作用。
    • 当然。首先,您要访问参数文本并完成,因此您必须检查它们是否存在,这样您就不会将 undefined 传递给您的 ToDo。我所说的逻辑是,例如,您是否需要完成发送的参数。我认为没有必要,所以如果没有发送,我将其设置为 false。您的测试之前失败的原因是因为在我之前的代码中,我假设如果参数 completed 丢失并且在测试中它丢失,它应该发送 400。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-20
    • 2010-12-03
    • 2012-09-15
    • 1970-01-01
    相关资源
    最近更新 更多