gpt4 book ai didi

mongodb - 用 hapijs stub mongoDB

转载 作者:行者123 更新时间:2023-11-28 20:51:05 25 4
gpt4 key购买 nike

我想弄清楚如何在 hapi js 中 stub mongoDB 以允许测试,但我不知道该怎么做。我试过检查 Sinonjs 但我不知道如何在这种特殊情况下应用它。下面是一些代码:

// index.js
'use strict';

const Hapi = require('hapi');
const MongoJS = require('mongojs');

const server = new Hapi.Server();

server.connection({ host: 'localhost', port: 11001 });

server.app.db = MongoJS('crunchbase', ['companies']);

server.register([
{
register: require('./lib/plugins')
},
{
register: require('./lib/modules/companies'),
options: {
baseUrl: '/v1/companies'
}
}
], (err) => {

if (err) {
throw err;
}

server.start((err) => {

if (err) {
throw err;
}
server.log('info', `Server listening on ${server.info.uri}`);
});
});

module.exports = server;

路线如下:

// companies.js
'use strict';

const Boom = require('boom');
const Joi = require('joi');

const error = Joi.object().keys({
statusCode: Joi.number(),
error: Joi.string(),
message: Joi.string()
});

const schema = Joi.object().keys({
_id: Joi.object(),
permalink: Joi.string(),
name: Joi.string(),
homepage_url: Joi.string(),
category_list: Joi.string(),
funding_total_usd: Joi.alternatives().try(Joi.number(), Joi.string()),
status: Joi.string(),
country_code: Joi.string().allow(''),
state_code: Joi.alternatives().try(Joi.string(), Joi.number()).allow(''),
region: Joi.string().allow(''),
city: Joi.string().allow(''),
funding_rounds: Joi.number(),
founded_at: Joi.string().allow(''),
first_funding_at: Joi.string(),
last_funding_at: Joi.string()
});

exports.register = (server, options, next) => {

const db = server.app.db;
const { baseUrl } = options;

server.route([
{
method: 'GET',
path: baseUrl,
config: {
description: 'companies',
notes: 'Get a list of companies from the database',
tags: ['api'],
validate: {
query: {
limit: Joi.number().min(1).max(20).default(5)
}
},
response: {
status: {
200: Joi.array().items(schema),
400: error,
500: error
}
}
},
handler: (request, reply) => {

db.companies.find().limit(request.query.limit, (err, docs) => {

if (err) {
return reply(Boom.wrap(err, 'Internal MongoDB error.'));
}
reply(docs);
});
}
}
]);

return next();
};

exports.register.attributes = {
pkg: require('./package.json')
};

这是测试套件:

// companies.test.js
'use strict';

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

const lab = exports.lab = Lab.script();
const { describe, it } = lab;
const expect = Code.expect;

const Server = require('../../');

describe('Companies module test suite', () => {

const baseUrl = '/v1/companies';

it('should return array of 5 companies by default', (done) => {

Server.inject({
method: 'GET',
url: baseUrl
}, (response) => {

expect(response.statusCode).to.equal(200);
expect(response.result).to.be.an.array().and.have.length(5);
done();
});
});

it('should return array of 3 companies', (done) => {

Server.inject({
method: 'GET',
url: baseUrl + '?limit=3'
}, (response) => {

expect(response.statusCode).to.equal(200);
expect(response.result).to.be.an.array().and.have.length(3);
done();
});
});

it('should throw an error', (done) => {

Server.inject({
method: 'GET',
url: baseUrl + '?limit=me'
}, (response) => {

expect(response.statusCode).to.equal(400);
expect(response.result.error).to.equal('Bad Request');
done();
});
});
});

它有效,但前提是存在与我想要解耦的数据库的连接。任何帮助将不胜感激。

最佳答案

这是 devinivy 提供的解决方案

One approach I've taken is to place queries in server methods, then stub out the server methods (server.methods.x = stubX) in my tests.

您还可以查看 proxyquire正如 timcosta 所建议的那样

这是简介 github discussion

关于mongodb - 用 hapijs stub mongoDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46742600/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com