Home > Software design >  Rails 5 rspec - convert object to ActiveRecord Relation
Rails 5 rspec - convert object to ActiveRecord Relation

Time:10-23

I have method that use .ids for ActiveRecord array. Something like that:

def exmpale(array)
  method2(array.ids)
end

Now I want to create tests for this method, but when I create objects by FactoryBot I dont get ActiveRecord array. For example:

let(:array) { FactoryBot.create(:class_example) }

I can use ClassExample.where to get have ActiveRecord Relation, but Im not sure it's good idea. Is there any other soultion without changing method?

CodePudding user response:

The simple way to do it is to use create_list and return an ActiveRecord relation, but if I were you I'd rethink it to use stubs.

let(:array) do
  list = create_list(:class_example, 4)
  ClassExample.where(id: list.map(&:id))
end
  • Related