我正在用mocha和chai(期望)构建一些测试。 保持简单,因为我正在学习测试方法。DB是Node.js中的保留字
我在配置文件中有一个mysql db层。 测试数据库参数,我遇到了一个奇怪的问题。
这些数据库参数测试精细:
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”作为变量。
UPDATE 真的很蠢,新手级别的错误。 所以专注于学习测试方法,我没有意识到我已经创建了'db'var作为'ini'的需求,然后在后面引用它,就好像它是唯一的。
真的很笨。不顾一切地奔向目的地,没有遵循一些好的方法。
'require('../db/config.ini')'是一个字符串的结果吗? (提示:不是根据错误消息。)你不会想调用某些* else *'db',因为(a)最后一个胜利,并且(b)它真的很混乱。这个问题突出。 –
好的。好点子。也许'ini'文件不是这里最好的方法。 我来自大多数Unix服务器背景。旧技术的想法仍然显现。 –
一个'ini'文件或不是不相关的(不是我会采用的方向,因为你只能使用一个对象),问题是隐藏一个现有的引用。 –