Mor*_*nor 1 mysql mocha.js node.js express
我想使用Mocha测试与我的数据库的连接,但似乎我做错了,因为无论凭据是什么,我总是让我的测试通过...
这是我使用的代码:
describe('Access to DB', function(){
describe('#fail', function(){
it('should return -1 because wrong credentials', function(){
var connection = mysql.createConnection({
host: 'right host',
user: 'wrong user',
password: 'wrong password',
database: 'right database'
});
connection.connect(function(err){
assert.equal(7,err.stack.indexOf("ER_ACCESS_DENIED_ERROR"));
});
});
})
});
Run Code Online (Sandbox Code Playgroud)
我在我的程序上测试了代码,当连接实际失败时,它会抛出错误.在这个错误中,我得到的indexOf(ER_ACCESS_DENIED_ERROR)等于7.
知道这一点,为什么我的测试总是通过,我如何纠正它以满足我的需要?
您需要告诉mocha您正在编写的测试是异步的.在it函数调用中添加完成回调,并从connection.connect调用此完成回调.完成的回调非常巧妙,可以确定错误是否作为第一个参数传递,如果传递错误,测试将失败.
describe('Access to DB', function(){
describe('#fail', function(){
it('should return -1 because wrong credentials', function(done){
var connection = mysql.createConnection({
host: 'right host',
user: 'wrong user',
password: 'wrong password',
database: 'right database'
});
connection.connect(done);
});
})
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2830 次 |
| 最近记录: |