小编Iqe*_*qen的帖子

使用 Sinon 模拟常量/变量?

我对测试还很陌生,甚至对 Sinon 也比较陌生。

在这里,我设置了一条快速路线:

import context = require("aws-lambda-mock-context");

this.router.post('/', this.entryPoint);

public entryPoint(req: Request, res: Response, next: NextFunction) {
    const ctx = context();
    alexaService.execute(req.body, ctx);
    ctx.Promise
        .then((resp: Response) => res.status(200).json(resp))
        .catch((err: Error) => res.status(500));
}
Run Code Online (Sandbox Code Playgroud)

我的目标是测试 post 调用是否/正确运行。我的测试脚本是:

describe('/POST /', () => {
    it('should post', () => {
        chai.request(app)
            .post('/v2')
            .end((err, res) => {
                expect(res).to.be.ok;
            });
    });
});
Run Code Online (Sandbox Code Playgroud)

虽然我的测试通过了它status: 500由于const ctx = context()未被识别而返回。是否有适当/正确的方法来监视变量ctx并使用 Sinon 在我的测试中返回模拟变量?我在这里转了这么久。

javascript unit-testing mocha.js sinon chai

5
推荐指数
1
解决办法
5833
查看次数

标签 统计

chai ×1

javascript ×1

mocha.js ×1

sinon ×1

unit-testing ×1