2017-02-24 59 views
0

我想单元测试(摩卡&柴)在我的node.js应用程序池连接(MySQL)。我不知道我是否以正确的方式使用测试,如果是这样,为什么他们总是被认定为“待定”。如何单元测试池连接?

给出以下代码,我将如何测试它?

var pool = mysql.createPool({ 
    connectionLimit: 100, 
    host: 'localhost', 
    user: 'userName', 
    password: 'userPass', 
    database: 'userDatabase', 
    debug: false 
}); 

我尝试了很多方法,但似乎没有工作。最好的我是这样的:

describe("Database", function() { 
    describe("Connection", function() { 
     it("Should connect without an error", pool.getConnection(function (err, connection) { 
      expect(err).to.be.null; 
      }) 
     ); 
    }) 
}); 

这将返回,如果凭据是正确的:

Express server listening on port 8080 
    Database 
    Connection 
     - Should connect without an error 


    0 passing (15ms) 
    1 pending 

,并返回,如果凭据不正确:

Express server listening on port 8080 
    Database 
    Connection 
     - Should connect without an error 
     1) Uncaught error outside test suite 


    0 passing (23ms) 
    1 pending 
    1 failing 

    1) Database Connection Uncaught error outside test suite: 
    Uncaught AssertionError: expected [Error: ER_DBACCESS_DENIED_ERROR: Access denied for user 'userName'@'localhost' to database 'userDatabase'] to be null 

预先感谢您。

+0

请注意,这是一个集成测试,而不是单元测试。它测试了两个系统在一起工作,而不是* your *应用程序中的单个功能单元独立工作。请参阅http://stackoverflow.com/questions/1217736/how-to-write-unit-tests-for-database-calls以测试数据库调用。 –

回答

2

你在第二个参数中传递给it的内容必须是一个函数。现在你正在做的:

it("Should connect without an error", pool.getConnection(...)) 

pool.getConnection采用了回调,以便在所有的可能性,它返回undefined。所以它看起来像这样摩卡:

it("Should connect without an error", undefined) 

这是一个悬而未决的测试,因为有undefined回调是告诉摩卡测试挂起的方式。您需要将您的电话打包到pool.getConnection的功能:

it("Should connect without an error", function (done) { 
    pool.getConnection(function (err, connection) { 
     if (err) { 
      done(err); // Mocha will report the error passed here. 
      return; 
     } 
     // Any possible tests on `connection` go here... 

     done(); 
    }); 
}); 
+0

如果'expect(err).to.be.null'抛出一个异常,'done()'不会被调用,因此测试在超时之前不会解析? –

+0

这是真的,但它通常不是一个问题。测试将通过的时间为99.9999%,超时不会被触发。尽管如此,我仍然编辑了我的答案,以表明如何立即通过。如果OP不会在回调中做任何其他测试,那么OP可以将它降低到你的答案中,但是如果有任何'连接'测试将被完成,那么应该检查'err'第一。 – Louis

+0

可以说我在对象构造函数中使用了'pool.getConnection()',并将查询结果中的值赋给了我的对象属性,'done()'调用会等到我的对象在调用'var user =新用户(1);'例如? – Faegy

1

参见testing asynchronous code在摩卡文档。你的it函数应该看起来类似于下面。

it('Should connect without an error', function (done) { 
    pool.getConnection(done); 
}); 

或者,如果你想添加您的回调中断言执行以下操作:

it('Should connect without an error', function (done) { 
    pool.getConnection((err, connection) => { 
    try { 
     expect(connection).to.not.be.null; 
     expect(connection).to.have.property('foo'); 
     done(); 
    } catch (error) { 
     done(error); 
    } 
    }); 
}); 

注意你最好应与承诺的测试,因为这允许您运行连接对象上expect报表,无需额外尝试/ catch/done语句。例如,如果pool.getConnection返回一个承诺,你可以这样做:

it('Should connect without an error', function() { 
    return pool.getConnection(connection => { 
    expect(connection).to.have.property('foo'); 
    // more assertions on `connection` 
    }); 
}); 

另外请注意,这些都不是“单元测试”,而是集成测试,因为他们正在测试两个系统一起工作,而不仅仅是你的应用程序运行得正如预期的那样。