我对测试比较陌生,对Sinon更是新手。
这里有一个Express路由设置:
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));
}
我的目标是测试对/
的post调用是否适当运行。我的测试脚本如下:
describe('/POST /', () => {
it('should post', () => {
chai.request(app)
.post('/v2')
.end((err, res) => {
expect(res).to.be.ok;
});
});
});
尽管我的测试通过了,但由于未能识别const ctx = context()
导致返回status: 500
。在使用Sinon时,是否有适当/正确的方法来监视变量ctx
并在我的测试中返回模拟变量?我一直在这里挣扎了很长时间。
mockery.registerAllowable
:) - Philippe Hebert