0

您好我目前正在尝试为我的 HTTP 请求学习和实施一些单元测试。目前我有一个 SQlite 数据库设置和一个简单的发布请求。这是我到目前为止写的测试:

"use strict";
process.env.NODE_ENV = 'test';

const chai = require('chai');
const chaiHttp = require('chai-http');
chai.use(chaiHttp);
const expect = chai.expect;

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

chai.request('http://localhost:8080')
    .put('/tempValue')
    .send({sensorid: '1', sensorValue: '25.00', timeRecorded: '2020-04-12 12:30'})
    .then(function (res) {
        expect(res).to.have.status(200);
    })
    .catch(function (err) {
        throw err;
    });


我似乎收到无法打开数据库文件的错误。目前我只是连接到 app.js 文件中的数据库,使用const dbPath = "./database/database.db";const dbConnection = sqlite.open(dbPath, { Promise });

我似乎也收到有关错误使用 .catch 的错误?这是错误日志:

(node:13952) UnhandledPromiseRejectionWarning: Error: SQLITE_CANTOPEN: unable to open database file
(node:13952) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unha
ndled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:13952) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:13952) UnhandledPromiseRejectionWarning: AssertionError: expected { Object (_events, _eventsCount, ...) } to have status code 200 but got 404
    at D:\Uni Work\3rd Year\302CEM - Agile Development\Penguin Project\test\api\temperature\get.js:15:29
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:13952) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unha
ndled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)

谢谢你的帮助!

编辑:

这是我正在测试的特定发布请求,它使用 MQTT 检查发送的消息是否发送到该主题结构,然后插入到数据库中。

app.ws.use(route.all('/tempValue', function (ctx){
    client.on('message', async function(topic,message){
        console.log(topic, message.toString());
        if (topic === '302CEM/Penguin/Room1/Temperature'){
            const SQL = "INSERT INTO sensorData (sensorid, sensorValue, timeRecorded) VALUES (?,?,?)";
            try {
                const temptostring = message.toString();
                const db = await dbConnection;
                await db.run(SQL, ["1",temptostring, moment().format('YYYY-MM-DD HH:mm')]);
                ctx.websocket.send(temptostring);
            } catch (err) {
                console.log(err);
            }
        }
    })
}));
4

0 回答 0