Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 如何使用Sinon与使用mssql库的数据库进行存根交互?_Sql Server_Node.js_Sinon_Node Mssql - Fatal编程技术网

Sql server 如何使用Sinon与使用mssql库的数据库进行存根交互?

Sql server 如何使用Sinon与使用mssql库的数据库进行存根交互?,sql-server,node.js,sinon,node-mssql,Sql Server,Node.js,Sinon,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; i

使用库从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   
    }
  });
});
您自然倾向于说“好吧,使用
createStubInstance
”,但当我使用它时,我得到的连接对象(
new sql.connection(config)
)中包含了繁琐的请求(库在连接中构建驱动程序对象时默认使用的对象),而不是我的存根请求。我在
sql
对象的任何地方都找不到冗长的请求来将其删除


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

嗯,我确实设法解决了这个问题,但出于某种原因,它感觉有点不对劲。可能是因为我从来没有想出如何存根新的sql.Request(conn)调用

理想情况下我希望在不必将sql库包含在我的
模块中的情况下实现此功能。导出
。我可以通过
请求
库来实现这一点,但在敲了几个小时这个库之后,我无法让它以同样的方式工作

第一:导出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;
  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;