gpt4 book ai didi

javascript - 如何在nodejs上为postgresql数据库编写单元测试

转载 作者:行者123 更新时间:2023-12-04 17:41:01 26 4
gpt4 key购买 nike

我正在尝试使用 postgresql 为我的 API 编写测试。

测试似乎没有命中正确的数据库以进行响应,或者我的配置可能不适合测试。

我希望测试检查我的 token 并将其视为来自正文的请求,但测试甚至无法识别请求正文 (req.body);

我需要指导。

我的文件如下。测试.js

/* global describe it */
import chai, { expect, assert } from 'chai';

import chaiHttp from 'chai-http';

import jwt from 'jsonwebtoken';

import server from '../app/app';

import parties from '../db/dummy';

process.env.NODE_ENV = 'test';

chai.should();
chai.use(chaiHttp);

const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiZW1haWwiOiJ0b3NpbkB5YWhvby5jb20iLCJpc19hZG1pbiI6dHJ1ZSwiaWF0IjoxNTQ5Mjc3MzAxLCJleHAiOjE1NDkzNjM3MDF9.ZJl147IVpw3TakPnR4uw_y6ZQZ6-Heoup5bdBBPs0iE';
const decoded = jwt.verify(token, process.env.SECRET_KEY);

describe('POST/api/v1/parties', () => {
it('should return 201 for new party', (done) => {
const newParty = {
name: 'APGC',
hqAddress: '1 Iyana Iba ',
logoUrl: 'www.logo.net.png',
decoded,
};
chai.request(server)
.post('/api/v1/parties')
.type('form')
.send(newParty)
.end((err, res) => {
expect(decoded).be.a('string');
expect(res.body).to.have.property('data');
assert.isOk(res.body);
});
done();
});
});

我的包.json

{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "nyc mocha --compilers js:@babel/register server/test/*.spec.js --exit",
"start:dev": "npm run build && npm run serve",
"start": "nodemon --exec babel-node server/app/app.js",
"heroku": "node server/app/app.js",
"build": "babel server -d lib",
"serve": "node lib/app/app.js",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"migration": "psql -U postgres -p 5433 -f server/model/table.sql",
"seed": "babel-node server/model/seeding.js",
"data:dev": "npm run migration && npm run seed"
},
"author": "darot",
"license": "ISC",
"dependencies": {
"bcryptjs": "^2.4.3",
"body-parser": "^1.18.3",
"dotenv": "^6.2.0",
"express": "^4.16.4",
"express-jwt": "^5.3.1",
"jsonwebtoken": "^8.4.0",
"morgan": "^1.9.1",
"pg": "^7.8.0"
},
"devDependencies": {
"@babel/cli": "^7.2.3",
"@babel/core": "^7.2.2",
"@babel/node": "^7.0.0",
"@babel/preset-env": "^7.3.1",
"@babel/register": "^7.0.0",
"babel-eslint": "^10.0.1",
"babel-jest": "^24.0.0",
"babel-loader": "^8.0.5",
"babel-preset-env": "^1.7.0",
"babel-register": "^6.26.0",
"chai": "^4.2.0",
"chai-http": "^4.2.1",
"codecov": "^3.1.0",
"coveralls": "^3.0.2",
"cross-env": "^5.2.0",
"eslint": "^5.12.1",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-plugin-import": "^2.15.0",
"istanbul": "^0.4.5",
"mocha": "^5.2.0",
"nodemon": "^1.18.9",
"nyc": "^13.1.0",
"travis": "^0.1.1"
}
}

我的数据库配置

import pg from 'pg';

import dotenv from 'dotenv';

dotenv.config();

process.env.NODE_ENV = 'test';

let connection;
const string = {
user: process.env.DB_USER,
host: process.env.DB_HOST,
database: process.env.DB_NAME,
password: process.env.DB_PASS,
port: process.env.DB_PORT,
max: 10,
idleTimeoutMillis: 3000,
};
const stringTest = {
user: process.env.DB_USER,
host: process.env.DB_HOST,
database: process.env.DB_NAME2,
password: process.env.DB_PASS,
port: process.env.DB_PORT,
max: 10,
idleTimeoutMillis: 3000,
};
if (process.env.NODE_ENV === 'production') {
connection = {
connectionString: process.env.DATABASE_URL,
ssl: true,
};
} else if (process.env.NODE_ENV === 'test') {
connection = stringTest;
} else {
connection = string;
}
const pool = new pg.Pool(connection);

export default pool;

最佳答案

我能够通过在测试套件中的 post 方法下设置授权来解决这个问题.set('授权', token)

关于javascript - 如何在nodejs上为postgresql数据库编写单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54515084/

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