我正在尝试为单元测试用例模拟 Cassandra,我正在使用 npm Cassandra-driver 来连接/查询 cassandra。为了模拟存根,这是我的代码。
beforeEach(function () {
let CassandraClient = sinon.stub();
CassandraClient.prototype.execute = sinon.stub().returns(new Promise((fulfill, reject) => {
console.log("Inside execute method");
fulfill(cassndaraExperimentQueryData);
}));
const CassandraClientWrapper = proxyquire('../../../../api/services/index.js', { 'cassandra-driver': { Client: CassandraClient } });
console.log(typeof CassandraClientWrapper);
CassandraClientWrapper.init();
});
这就是我的 index.js 的样子
const cassandra = require('cassandra-driver');
module.exports = class DBServices {
init(){
const contactPoint = process.env['cassndraIP'] || '127.0.0.1';
var casssandraClient = new cassandra.Client({
contactPoints: [contactPoint]
});
let persistentObject = new persistent(redisMockClient, casssandraClient)
}
}
但这里不是创建模拟对象,而是从连接到实际数据库的 Cassandra-driver 创建对象。