简体   繁体   English

Nodejs api rest 使用 mocha 和 chai 进行测试。 发布待定

[英]Nodejs api rest test with mocha and chai. Post pending

I'm learning to do api rest tests and I came across a problem using chai and mocha, I was following this example .我正在学习做 api rest 测试,我遇到了使用 chai 和 mocha 的问题,我正在关注这个例子

The problem is that the POST method is always pending, according to something pending it does not mean that it failed.问题是 POST 方法总是挂起,根据挂起的东西并不意味着它失败了。 But I would like to pass this test.但是我想通过这个测试。
My route is returning a json of what was created, so I didn't understand why the test didn't pass, can someone help me with that?我的路线返回创建内容的json ,所以我不明白为什么测试没有通过,有人可以帮助我吗?

Link to the repository if it helps.如果有帮助,请链接到存储库

POST route code POST路由代码

router.post("/game", async (req, res) => {
    const title = req.body.title
    const year = req.body.year
    const price = req.body.price
    try {
       const gameCreated = await Game.create({
            title: title,
            year: year,
            price: price
        })
        res.json(gameCreated)
        
    } catch (err) {
        console.log(err)
        res.sendStatus(500)
    }
})

Test code测试代码

describe("POST game test", () => {
    it("must create a new game"), (done) => {

        let game = {
            title: "Game created by mocha",
            year: 2020,
            price: 178
        }
        chai.request('localhost:3033')
            .post('/game')
            .send(game)
            .end((err, res) => {
                res.should.have.status(200)
                
                done()
            })
    }
})

You have a typo in your test, you have to write like this:你的测试有错字,你必须这样写:

it("must create a new game", (done) => {

But you have但是你有

 it("must create a new game"), (done) => {

Note the ) different注意)不同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM