如何在一个API中为两个await函数创建测试

时间:2019-01-21 18:57:21

标签: unit-testing express mocha chai

我在一个API中调用了两个函数。我曾经使用await来摆脱嵌套结构。我的测试用例无效,拒绝测试无效

这是可以使用的API,但测试无效



router.put('/:id',
    insertAuthorIdToBody, validateSchema(calculatorSchema),
    validateBodyIdMatchesParam('id'), async (req, res, next) = {
        try {
            await addTags(req.body.tags);
            await addCalculator(req.body);
        } catch (err) {
            next(err)
        }
        res.status(201).json(req.body)
    });

   describe('PUT /:id', () = {
    it('should return calculator on success request', async () = {

        addCalculator.resolves(validCalculator);
        addTags.resolves(validCalculator.tags);

        await request(app)
        .put('/new-calculator-uuid')
        .send(validCalculator)
        .set('Accept', 'application/json')
        .expect(201);

        await request(app)
        .put('/new-calculator-uuid')
        .send(validCalculator)
        .set('Accept', 'application/json')
        .expect(201, {
            ...validCalculator,
            author: user.id
        });
    });
    it('should return status 500 on server error', async () = {
        addCalculator.rejects();

        await request(app)
        .put('/new-calculator-uuid')
        .send(validCalculator)
        .set('Accept', 'application/json')
        .expect(500);
    });
});

0 个答案:

没有答案