如何使用 Sinon 通过 mssql 库存根与数据库的交互?

How do I use Sinon to stub interaction with a database using mssql library?

使用 node-mssql 库从 SQL 中提取数据。我已经使用 Sinon 一段时间了(用它编写了大约 200 个测试);在思考如何将这个库存根时遇到了很多麻烦。代码如下所示:

var sql = require('mssql');
var conn = new sql.Connection(sqlConfig); // sqlConfig is connection info, defined elsewhere 

conn.connect(function(err) {
  var req, selectFromTable;
  if (err != null) {
    // handle error
  }
  req = new sql.Request(conn);
  selectFromTable = "select * from DW." + table + " where DWCreatedDate >= '" + start + "' and DWCreatedDate <= '" + end + "' ";
  logger.debug("Selecting with: ", selectFromTable);
  req.input('statement', sql.NVarChar, selectFromTable);
  return req.execute('sp_executesql', function(err, results, returnValue, affected) {
    if (err != null) {
      // etc.
    } else {
      // data processing   
    }
  });
});

代码运行良好。现在我正在尝试为它编写一个测试。我知道这个库很难测试,所以我拖延了。我最接近的代码:

var conn, reqExecute, sqlReqStub; 
sqlReqStub = sinon.stub(); 
sqlReqStub.execute = sinon.stub(); 
sinon.stub(sql, 'Request').returns(sqlReqStub); 
conn = sinon.stub(); 
sinon.stub(sql, 'Connection').returns(conn);

conn.connect = sinon.stub().callsArgWith(0, null);

reqExecute = sqlReqStub.execute.withArgs('sp_executesql').onFirstCall().callsArgWith(1, null, {
  a: 1
});

您的自然倾向可能是说 "well, use createStubInstance" 但是当我使用它时,我会取回具有 TediousRequest 的连接对象 (new sql.Connection(config))(库在构建驱动程序对象时的默认设置)在连接内部)而不是我的存根请求。我无法在 sql 对象中的任何地方找到 TediousRequest 以将其存根。

我被困在这里;希望有人有一些代码可以做到这一点,或者可以解释我做错了什么。

好吧,我确实设法解决了它,但由于某种原因感觉有点老套。可能是因为我从来没有想出如何存根 new sql.Request(conn) 调用。

理想情况下 我想让它工作而不必在我的 module.exports 中包含 sql 库。我可以使用 request 库来做到这一点,但是在这个库上敲了几个小时后,我无法让它以同样的方式工作。

首先:导出 sql 库:

sql = require('mssql')
// much code
module.exports.sqlLib = sql

第二个:存根东西:

  var connection, sqlReqStub, sqlStubLib;
  var server = require('./index.js')    
  sqlStubLib = {};    
  connection = new EventEmitter();    
  connection.connected = true;    
  connection.close = function() {};    
  connection.connect = sinon.stub().callsArgWith(0, null);    
  sqlStubLib.Connect = sinon.stub();    
  sqlStubLib.Connection = function() {
    return connection;
  };    
  sqlReqStub = sinon.stub();    
  sqlReqStub.input = function() {};    
  sqlReqStub.execute = sinon.stub();    
  sqlReqStub.execute.withArgs('sp_executesql').onFirstCall().callsArgWith(1, null, [
    [
    // js object 
    ]
  ], null, null);

  sqlStubLib.Request = function() {
    return sqlReqStub;
  };

  server.sqlLib = sqlStubLib;