简体   繁体   中英

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 .

The problem is that the POST method is always pending, according to something pending it does not mean that it failed. 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?

Link to the repository if it helps.

POST route code

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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