我有使用.ids
为ActiveRecord数组的方法。像这样:
def exmpale(array)
method2(array.ids)
end
现在我想为这个方法创建测试,但是当我通过FactoryBot创建对象时,我没有得到ActiveRecord数组。例如:
let(:array) { FactoryBot.create(:class_example) }
我可以使用ClassExample.where
来获得ActiveRecord关系,但我不确定这是个好主意。在不改变方法的情况下,有没有其他的解决方法?
最简单的方法是使用create_list
并返回ActiveRecord
关系,但如果我是你,我会重新考虑使用stubs
。
let(:array) do
list = create_list(:class_example, 4)
ClassExample.where(id: list.map(&:id))
end