【问题标题】:Nodejs api rest test with mocha and chai. Post pending使用 mocha 和 chai 进行 Nodejs api 休息测试。待发帖
【发布时间】:2020-11-13 21:50:57
【问题描述】:

我正在学习做 api rest 测试,我在使用 chai 和 mocha 时遇到了问题,我关注了 this example

问题是 POST 方法总是挂起的,根据挂起的东西并不意味着它失败了。但我想通过这个测试。
我的路线返回了 json 的创建内容,所以我不明白为什么测试没有通过,有人可以帮我吗?

如果有帮助,请链接到 repository

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

测试代码

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

【问题讨论】:

    标签: node.js rest mocha.js integration-testing chai


    【解决方案1】:

    你的测试有错别字,你必须这样写:

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

    但你有

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

    注意) 不同

    【讨论】:

    • 谢谢你的朋友,你帮助了我,现在它正在工作!因为我在 getById 中遇到了不同的错误,所以我将打开另一个主题。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2017-02-18
    • 2021-09-14
    • 1970-01-01
    • 2016-09-20
    • 1970-01-01
    • 1970-01-01
    • 2015-10-01
    • 1970-01-01
    相关资源
    最近更新 更多