ruby on rails - Custom range with FactoryGirl sequence -
in old version possible (more examples):
factory.sequence(:email, 50..60) |n| "user_#{n}@example.com" end
using in new factorygirl version create nomethoderror: undefined method 'next' 0..10:range
.
in particular have model user
has multiple time_entries
. want create default values. simplicity each user should create n
time entries. entries created this:
factorygirl.define factory :time_entry start_time { generate(:time_entry_begin) } end_time { generate(:time_entry_end) } end end factorygirl.define sequence(:time_entry_begin, 0..10) |n| datetime.now - n.hours end end factorygirl.define sequence(:time_entry_end, 0..10) |n| datetime.now - n.hours + (1 + n).minutes end end
you have pass object responds next
, e.g. enumerator:
factorygirl.define factory :user sequence(:email, (50..60).cycle) { |n| "user_#{n}@example.com" } end end factorygirl.build(:user) #=> <#user @email="user_50@example.com"> factorygirl.build(:user) #=> <#user @email="user_51@example.com"> factorygirl.build(:user) #=> <#user @email="user_52@example.com"> # ... factorygirl.build(:user) #=> <#user @email="user_59@example.com"> factorygirl.build(:user) #=> <#user @email="user_60@example.com"> # `cycle` start over: factorygirl.build(:user) #=> <#user @email="user_50@example.com">
Comments
Post a Comment