gpt4 book ai didi

javascript - Mocha 测试相同对象的assert.equal 失败

转载 作者:行者123 更新时间:2023-12-02 23:39:07 25 4
gpt4 key购买 nike

我有一些 Mocha 测试未能通过 assert.equal,尽管这两个项目似乎是相同的。我可能不明白测试是如何运行的,但即使错误输出也显示了 2 个类似的内容:

states.js

const sqlite3 = require('sqlite3').verbose();
const util = require('util');

async function getDB() {
return new Promise(function(resolve, reject) {
let db = new sqlite3.Database('./project.db', (err) => {
if (err) {
console.error(err.message);
reject(err)
} else {
console.log('Connected to the project database.');
resolve(db)
}
});
return db
});
}

exports.getDB = getDB

try {
// run these statements once to set up the db
// let db = getDB();
// db.run(`CREATE TABLE services(id INTEGER PRIMARY KEY, service text, date text)`);
// db.run(`INSERT INTO services(id, service, date) VALUES (1, 'blah', '01-23-1987')`)
} catch(err) {
console.log(err)
}

// is working
exports.get = async function(service) {
function getResults(service) {
return new Promise(async function (resolve, reject) {
const db = await getDB();
let sql = `SELECT Id id,
Service service,
Date date
FROM services
WHERE service = ?`;
db.get(sql, [service], (err, row) => {
if (err) {
console.error(err.message);
reject(err)
} else {
if (row) {
let this_row = {'id': row.id, 'service': row.service, 'date': row.date};
this_row ? console.log(row.id, row.service, row.date) : console.log(`No service found with the name ${service}`);
resolve(this_row)
} else {
resolve(null)
}
}
})
});
}
let row = await getResults(service)
return row
}


let row = exports.get('blah')

console.log("Printing out what get returns:")
console.log(row)


exports.set = async function(service, state) {
function setResults(service) {
return new Promise(async function (resolve, reject) {
const db = await getDB();
let sql = `UPDATE services
set id = ?,
service = ?,
date = ?
WHERE service = ?`;
try {
db.run(sql, [state.id, service, state.date, service]);
resolve();
} catch(err) {
reject(err);
}
});
}
await setResults(service)
}

exports.set('blah', {'id': 1, 'date': '01-23-1987'})

exports.clear = async function(service) {
function deleteResults(service) {
return new Promise(async function (resolve, reject) {
const db = await getDB();
let sql = `DELETE from services
WHERE service = ?`;
db.run(sql, [service]);
});
}
await deleteResults(service)
}

测试.js

const mocha = require('mocha');

const assert = require('assert');
const expect = require('chai').expect;
const should = require('chai').should();

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

let deletion_sql = `DELETE from services WHERE service = ?`;

it("get() should return the expected row", async function() {
let db = await state.getDB()
await db.run(deletion_sql, 'blah')
await db.run(`INSERT INTO services(id, service, date) VALUES (1, 'blah', '01-23-1987')`)
let result = await state.get('blah')
console.log("get test result is")
console.log(result)
assert.equal(result, { 'id': 1, 'service': 'blah', 'date': '01-23-1987' })
});


it("set() should set to new values as expected", async function() {
let db = await state.getDB()
await db.run(deletion_sql, 'blah')
await db.run(`INSERT INTO services(id, service, date) VALUES (1, 'blah', '01-23-1987')`)
await state.set('blah', {'id': 5, 'date': '05-22-2222'})
let result = await state.get('blah')
console.log("set test result is")
console.log(result)
assert.equal(result, { 'id': 5, 'service': 'blah', 'date': '05-22-2222' })
});

it("clear() should delete row from db", async function() {
await state.clear('blah')
let result = await state.get('blah')
assert.equal(result, null)
})

输出如下:

get test result is
{ id: 1, service: 'blah', date: '01-23-1987' }
1) get() should return the expected row
5 'blah' '05-22-2222'

set test result is
{ id: 5, service: 'blah', date: '05-22-2222' }
2) set() should set to new values as expected

3) clear() should delete row from db

0 passing (2s)
3 failing

1) get() should return the expected row:

AssertionError [ERR_ASSERTION]: { id: 1, service: 'blah', date: '01-23-1987' } == { id: 1, service: 'blah', date: '01-23-1987' }
+ expected - actual


at Context.<anonymous> (src\tests\testStates.js:26:12)
at process._tickCallback (internal/process/next_tick.js:68:7)

2) set() should set to new values as expected:

AssertionError [ERR_ASSERTION]: { id: 5, service: 'blah', date: '05-22-2222' } == { id: 5, service: 'blah', date: '05-22-2222' }
+ expected - actual

当项目实际上不同时,它通常会在错误下方显示带有 + 和 - 的差异,但这里预期和实际为空白,并且 == 显示测试 1 和 2 的 2 个相同项目。为什么会断言.相同项目的相同测试失败?这些元素一定是我遗漏了一些东西。它们是否不同,因为它仍然将其表示为 promise 而不是对象?我确实对两者都使用了 await 关键字。

最佳答案

我建议您更改断言以使用 chai 的 deepEqual()比较 JavaScript 对象时。因此,您可以比较对象,以便它们具有相同的键和相同的值。

如果您使用 equal()方法就像您在示例中使用 == 比较实际对象和预期对象一样。通常,在比较 JavaScript 对象时,这不是您感兴趣的内容(除非您要比较引用同一对象的两个变量,但我建议您改用 strictEqual())。

关于javascript - Mocha 测试相同对象的assert.equal 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56156157/

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