dimanche 22 novembre 2015

Rspec mock in each block for bulk action from params?

I have a controller method to delete items in bulk, based on checking checkboxes on my items index page, which get sent in as params. This occurs inside a transaction, so if any fails, none get deleted.

def bulk_delete_items
  item_ids = params[:item_ids]
  ActiveRecord::Base.transaction do
    item_ids.each do |id|
      item = Item.find(id)
      item.destroy!
    end
  end
end

I am trying to write a controller test for this method, and I can't figure out how to properly mock it. If I use FactoryGirl to build 2 items: item_1 and item_2... Do I mock the params to return the ids of those 2 items? Is there a way to specify that the first time through my each it should find item_1 and the second time through it should find item_2?

Aucun commentaire:

Enregistrer un commentaire