Rspec 如何在不产生大量重复数据的情况下测试长链方法的参数

Rspec 如何在不产生大量重复数据的情况下测试长链方法的参数,rspec,Rspec,我有一个活动记录查询,如下所示: user.templates.where(id: @template_id).joins(:another_record).pluck('templates.id').uniq 但是我只想测试where是否被正确的id调用。有没有一种方法可以做到这一点,而不必创建几个只响应其余方法的double 基本上我想要的是 expect_any_instance_of(User).to receive_message_chain('templates.where').w

我有一个活动记录查询,如下所示:

user.templates.where(id: @template_id).joins(:another_record).pluck('templates.id').uniq
但是我只想测试where是否被正确的id调用。有没有一种方法可以做到这一点,而不必创建几个只响应其余方法的double

基本上我想要的是

expect_any_instance_of(User).to receive_message_chain('templates.where').with(id: target_id).and_return(a_double_that_returns_another_double_that_responds_to_anything)
您需要一个:

顺便说一句,我看不出这个测试有什么好处。最好做一个集成测试,连接到一个真实的数据库,并检查您的查询是否实际工作,而不仅仅是调用一些AR方法


如果你能给我展示一下你测试的内容,我可以给你一个更周到的建议。

谢谢!让我试着把它放进去,看看使用它是否有意义。我还将尝试对其进行一点修订,以便提供更多的上下文。
a_double_that_returns_another_double_that_responds_to_anything = double.as_null_object
expect_any_instance_of(User).to receive_message_chain('templates.where').with(id: target_id).and_return(a_double_that_returns_another_double_that_responds_to_anything)