gpt4 book ai didi

ruby-on-rails - Rspec 期望方法接收 ActiveRecord::Relation

转载 作者:行者123 更新时间:2023-12-02 21:07:25 25 4
gpt4 key购买 nike

我有一个方法

foo(bar)

bars 是一个 ActiveRecord::Relation

在我的规范中,我想断言 foo 是用 bars 调用的,但在规范中 bars 只是一个数组

let(:bar) { create(:bar) }
let(:bars) { [bar] }

expect(described_class).to receive(:foo).with(bars)

有什么办法可以实现吗?我无法在 Controller 中对 bars 进行 stub ,因为我正在根据传递的 params 测试 bars 的过滤。

最佳答案

您可以对某些东西设定期望值,但仍然让它运行原始代码。只需将 and_call_original 添加到您的期望中即可:

expect(described_class).to receive(:foo).with(bars).and_call_original

此处提供的文档的详细信息:http://www.relishapp.com/rspec/rspec-mocks/v/3-4/docs/configuring-responses/calling-the-original-implementation

关于ruby-on-rails - Rspec 期望方法接收 ActiveRecord::Relation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35367689/

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