bonhoffer
bonhoffer

Reputation: 1473

FactoryGirl with local variables

I'm trying to create a time_slot at some random time in the future:

FactoryGirl.define do
  factory :time_slot do
    week_num = (rand(10) +1)
    day_num = (1+rand(30))
    hour_num = (1+rand(12))
    future_date = week_num.weeks.from_now
    my_time = Date.local(future_date.year,future_date.month,day_num, hour_num)
    sold_out false
    fitness_camp
    start_time {my_time}
    end_time {my_time + 8.weeks}

But this results in a undefined method + for #<FactoryGirl::Declaration::Static

I know this would work in one huge line, but there has to be a clean way to do this. It is confusing to have local variables mixed with attributes, so is the best practice to put this function somewhere else and generate the Factory by overriding the default date?

Thanks,

tim

Upvotes: 1

Views: 2438

Answers (1)

Logan Buesching
Logan Buesching

Reputation: 31

Use Random.rand() instead of just rand()

 FactoryGirl.define do
   factory :time_slot do
     week_num = (Random.rand(10) +1)
     day_num = (1+Random.rand(30))
     hour_num = (1+Random.rand(12))
     future_date = week_num.weeks.from_now
     my_time = Date.local(future_date.year,future_date.month,day_num, hour_num)
     sold_out false
     fitness_camp
     start_time {my_time}
     end_time {my_time + 8.weeks}

Upvotes: 3

Related Questions