{return car1 }); 但是现在我的函数中有一行我-6ren">
gpt4 book ai didi

javascript - 我如何在 Sinon 中存入一系列方法?

转载 作者:可可西里 更新时间:2023-11-01 02:31:45 24 4
gpt4 key购买 nike

我知道如何使用 stub 来替换一个函数。

sandbox.stub(Cars, "findOne",
() => {return car1 });

但是现在我的函数中有一行我想测试我需要 stub 看起来像这样

Cars.find().fetch()

所以这里有一个功能链,我不确定我需要做什么。如何 stub “查找”以返回可用于 stub “获取”的内容?

最佳答案

恕我直言,我们可以使用 returns 来做到这一点。我们不需要使用 callsFake 或将其模拟为函数。

// Cars.find().fetch()

sinon.stub(Cars, 'find').returns({
fetch: sinon.stub().returns(anything)
});

以防万一,如果在fetch()之后还有其他方法,我们可以使用returnsThis()

// Cars.find().fetch().where()

sinon.stub(Cars, 'find').returns({
fetch: sinon.stub().returnsThis(),
where: sinon.stub().returns(anything)
});

引用: https://sinonjs.org/releases/v6.3.3/

希望对你有帮助

关于javascript - 我如何在 Sinon 中存入一系列方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37948135/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com