42
Given a standard has_many relationship between two objects. For a simple example, let's go with:
class Order < ActiveRecord::Base
has_many :line_items
end
class LineItem < ActiveRecord::Base
belongs_to :order
end
What I'd like to do is generate a stubbed order with a list of stubbed line items.
FactoryGirl.define do
factory :line_item do
name 'An Item'
quantity 1
end
end
FactoryGirl.define do
factory :order do
ignore do
line_items_count 1
end
after(:stub) do |order, evaluator|
order.line_items = build_stubbed_list(:line_item, evaluator.line_items_count, :order => order)
end
end
end
The above code does not work because Rails wants to call save on the order when line_items is assigned and FactoryGirl raises an exception:
RuntimeError: stubbed models are not allowed to access the database
So how do you (or is it possible) to generate an stubbed object where it's has_may collection is also stubbed?