向服务器发送无效数据时,Mocha 测试失败

Mocha test failing when sending invalid data to the server

我正在使用 nodejs 和 mongodb 开发一个小型待办事项应用程序。

我这里有模型定义:

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

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

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

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));
            });
    });

你应该在使用前检查 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);
            })
    }
});

同样在您的架构中,它应该是 "required" 而不是 "require"