如何使用Sinon在Hapi/Lab测试中消除knex调用?

问题描述 投票:0回答:2

我正在尝试为新的 Hapi 应用程序设置测试模式。我过去曾在 Express 中使用过 Mocha 和 Chai,但我正在尝试使用 Lab 和 Code 来留在 Hapi 生态系统中。我还使用 Bookshelf 和 Knex 来处理数据库交互。

所以我想测试一个简单的健康终点。

'use strict';

const controller = require('../controller/healthController');

module.exports = [
  {
    method: 'GET',
    path: '/health',
    config: {
      handler: controller.health,
      description: 'The health endpoint returns 200',
      tags: ['api', 'health']
    }
  }
];

在处理程序中,它只是执行快速查询以确保它可以连接到数据库。

'use strict';

const bookshelf = require('../config/db');
const knex = bookshelf.knex;

module.exports = {
  health: function (request, reply) {
    knex.raw('SELECT version()').then(() => {
      reply('service is running');
    }).catch((err) => {
      reply({err: err, code: 500});
    });
  }
};

据我了解,需要服务器然后使用 server.inject 实际上并没有启动服务器,所以我不认为我应该有一个数据库连接,这意味着我应该在数据库调用中模拟它。对我来说奇怪的是这个测试通过了:

'use strict';
const Code = require('code');
const Lab = require('lab');

const lab = exports.lab = Lab.script();
const describe = lab.describe;
const it = lab.test;
const expect = Code.expect;
const before = lab.before;

let server;

describe('health controller', () => {

  before((done) => {
    server = require('../../server');
    done();
  });

  it('health check replies 200 when successful call to db', (done) => {

    const options = {
      method: 'GET',
      url: '/health'
    };

    server.inject(options, (res) => {
      expect(res.payload).to.include('is running');
      expect(res.statusCode).to.equal(200);

      done();
    });

  });

});

所以我有两个问题。首先,我觉得上面的测试不应该真正通过。除非它加载所有内容并因此连接到我想的数据库。也许我应该只测试控制器/处理程序方法?但我还没有找到任何例子。

其次,我尝试将 knex.raw 调用存根,当我尝试像下面那样执行时,我收到 500 错误。

'use strict';
const Code = require('code');
const Lab = require('lab');
const Sinon = require('sinon');

const lab = exports.lab = Lab.script();
const describe = lab.describe;
const it = lab.test;
const expect = Code.expect;
const before = lab.before;

let server;
let knex = require('../../app/config/db').knex;

describe('health controller', () => {

  before((done) => {
    server = require('../../server');
    done();
  });

  it('health check replies 200 when successful call to db', (done) => {

    const stub = Sinon.stub(knex, 'raw').returns({});

    const options = {
      method: 'GET',
      url: '/health'
    };

    server.inject(options, (res) => {
      expect(res.payload).to.include('is running');
      expect(res.statusCode).to.equal(200);
      expect(stub.calledOnce).to.be.true();
      done();
    });

  });

});

我不太确定为什么会发生这种情况。

sinon knex.js hapi.js bookshelf.js hapi.js-lab
2个回答
3
投票

我认为

Sinon.stub(knex, 'raw').resolves({});
是更好的解决方案


1
投票

server.inject
完全正常工作就像您向真实服务器发出请求一样。因此,如果运行测试时您的数据库已启动,端点将从数据库返回数据,就像您手动启动服务器时一样。

Sinon.stub(knex, 'raw').returns({});
不起作用。
knex.raw(…)
预计返回一个 Promise,而不是一个空对象。请尝试以下操作:

Sinon.stub(knex, 'raw').returns(Promise.resolve({}));

附注:请记住在每次测试后调用

server.stop()
,以确保测试之间没有持续的状态。一般来说,我认为您可以查看Hapi大学存储库中的示例测试文件。

© www.soinside.com 2019 - 2024. All rights reserved.