【问题标题】:How to inject a stub function when using Hapi.js server.inject使用 Hapi.js server.inject 时如何注入存根函数
【发布时间】:2022-01-22 01:03:11
【问题描述】:

我有一个使用 hapi-mongodb 插件的 hapijs 项目。

在处理程序中,我使用 hapi-mongodb 插件进行数据库调用。见下文

internals.getById = async (request, h) => {

    try {

        const db = request.mongo.db;
        const ObjectId = request.mongo.ObjectID;

        const query = {
            _id: ObjectId(request.params.id)
        };

        const record = await db.collection(internals.collectionName).findOne(query);

        //etc.....


我希望能够使用 server.inject() 对其进行测试,但我不确定如何存根 request.mongo.db 和 request.mongo.ObjectID

it('should return a 200 HTTP status code', async () => {

        const server = new Hapi.Server();

        server.route(Routes); //This comes from a required file

        const options = {
            method: 'GET',
            url: `/testData/1`
        };

        //stub request.mongo.db and request.mongo.ObjectID
        
        const response = await server.inject(options);

        expect(response.statusCode).to.equal(200);
    });

有什么想法吗?

【问题讨论】:

    标签: mongodb hapijs


    【解决方案1】:

    我解决了这个问题并意识到 mongo 插件装饰了可以存根的服务器对象。

    【讨论】:

      猜你喜欢
      • 2014-10-08
      • 1970-01-01
      • 1970-01-01
      • 2020-12-10
      • 1970-01-01
      • 2020-01-30
      • 1970-01-01
      • 2023-03-29
      • 2016-11-26
      相关资源
      最近更新 更多