DB是Node.js中的保留字吗

Is DB a reserved word in Node.js

我正在用 mocha 和 chai 构建一些测试(预计)。 保持简单,因为我正在学习测试方法。

我在配置文件中有一个 mysql 数据库层。 测试数据库参数,我 运行 遇到了一个奇怪的问题。

这些数据库参数测试正常:

host= 'localhost',
user='foo',
password='bar',

测试:

var expect = require('chai').expect;
var db = require('../db/config.ini');

describe('Database Access', function() {
    it('HOST parameter should be a string', function() {
        expect(host).to.be.a('string');
    });
    it('USER parameter should be a string', function() {
        expect(user).to.be.a('string');
    });
    it('PASSWORD parameter should be a string', function() {
        expect(password).to.be.a('string');
    });
    it('DB parameter should be a string', function() {
        expect(db).to.be.a('string');
    });
    it('HOST parameter should equal localhost', function() {
        expect(host).to.equal('localhost');
    });
    it('USER parameter should equal foo', function() {
        expect(user).to.equal('foo');
    });
    it('PASSWORD parameter should equal bar', function() {
        expect(password).to.equal('bar');
    });
    it('DB parameter should equal thatone', function() {
        expect(context).to.equal('thatone');
    });
});

当我添加数据库选择时,

db='thatone';

测试未通过参数,因为它将参数作为对象读取。

  1) Database Access DB parameter should be a string:
     AssertionError: expected {} to be a string
      at Context.<anonymous> (test/db_tests.js:21:20)

如果我将变量名称更改为 "context",测试将按预期通过。

我想知道使用 "db" 作为变量是否有明显的遗漏。

更新 真是愚蠢,新手级别的错误。 如此专注于学习测试方法我没有意识到我已经创造了 'db' var 作为对 'ini' 的要求,然后稍后引用它,就好像它是唯一的一样。

真傻。鲁莽地冲到目的地,没有遵循一些好的方法。

执行结果不是字符串:

var db = require('../db/config.ini');

您似乎正在尝试获取某个 INI 方言中的文件,以便 Node 对其进行有意义的解释。 节点默认不支持这个。如果你在加载它时没有收到错误,最可能的原因是你在那里的文本恰好也是有效的JavaScript 但由于 INI 文件不包含导出某些内容的正确代码(即文件不包含 exports.db = "something"module.exports = { ... } 或类似内容),因此模块的值为 {}.

您需要添加多个 npm 包之一,它将自动解释 INI 文件并提供有意义的值。我不能推荐一个,因为我不在我的软件中使用 INI 文件,但你可以 search npm 为你执行翻译的包。