Javascript 如何使用sinnon模拟bookshelf js中的回调函数

Javascript 如何使用sinnon模拟bookshelf js中的回调函数,javascript,sinon,knex.js,bookshelf.js,Javascript,Sinon,Knex.js,Bookshelf.js,我想模拟一下这段代码,它将bookshelf js(带knex)与sinon一起使用 const活动=等待模型.Campaign.forge() .query((qb)=>{ qb.其中(“账户id”,账户id); qb.andWhere(“状态”,models.Campaign.status.ACTIVE); 在哪里( “受众\创建\触发”, models.Campaign.publications\u CREATE\u TRIGGER.ON\u ENTER ); }) .fetchAll()

我想模拟一下这段代码,它将bookshelf js(带knex)与sinon一起使用

const活动=等待模型.Campaign.forge()
.query((qb)=>{
qb.其中(“账户id”,账户id);
qb.andWhere(“状态”,models.Campaign.status.ACTIVE);
在哪里(
“受众\创建\触发”,
models.Campaign.publications\u CREATE\u TRIGGER.ON\u ENTER
);
})
.fetchAll();
如何模拟.query函数中的内部查询。 我有点迷路了


多谢各位

我终于用Sinon解决了这个问题。这段代码涵盖了几乎所有的行为

const assert=require(“chai”).assert
常数sinon=要求(“sinon”)
常量模型=需要(“../../../../models”)
常量查询={
查询(func){}
}
常数qb={
其中(arg1,arg2){},
andWhere(arg1,arg2){}
}
const fetchAll={async fetchAll(){}
const forgeStub=sinon.stub(models.Campaign,“forge”)。返回(查询)
常数qbWhereStub=sinon
.存根(qb,“其中”)
.withArgs(“账户id”,账户id)
.returns(null)
const qbandherestub=sinon.stub(qb,“andWhere”)。返回值(null)
const querysub=沙箱
.stub(查询,“查询”)
.callsArgWith(0,qb)
.returns(fetchAll)
const fetchAllStub=sandbox.stub(fetchAll,“fetchAll”).returns(campaigs)
//调用方法
//核实
assert.equal(qbWhereStub.callCount,1)
assert.equal(QBandWhere stub.callCount,2)
assert.equal(forgeStub.callCount,1)
assert.equal(querysub.callCount,1)
assert.equal(fetchAllStub.callCount,1)
assert.isTrue(qbWhereStub.getCall(0).calledWithjustice(“account_id”,accountId))
assert.isTrue(qbAndWhereStub.getCall(0.CalledWithJustice)(“状态”,models.Campaign.status.ACTIVE))
阿塞特·伊斯特鲁(
QBandWhere存根
.getCall(1)
.calledWithjustice(“受众创建触发器”,models.Campaign.受众创建触发器。ON\u ENTER)
)
尝试使用设置测试友好型knex实例

测试看起来会简单得多

从“knex”导入knex;
从“knex mock client”导入{MockClient,getTracker};
描述(“测试”,()=>{
让db;
让跟踪器;
以前(()=>{
db=knex({client:MockClient});
tracker=getTracker();
});
每次(()=>tracker.reset())之后;
它(“应该做点什么”,()=>{
追踪器
.选择(
(查询)=>
query.sql.includes(“表名称”)和&query.bindings.includes(accountId)
)
.回应([]);
//使用数据库执行查询;
expect(tracker.history.select).toHaveLength(1);
});
});

您可能试图解决错误的问题。为什么要模拟
。where
?我想确保调用此SQL子句。如果未来开发更改了本条款,则测试将涵盖该条款