I have a model that contains two integer columns representing start_time
and end_time
. These times are represented by the minute offset for that day.
class Example < ActiveRecord::Base
validates :start_time, inclusion: 0..1440
validates :end_time, inclusion: 0..1440
end
I also have a factory
FactoryGirl.define do
factory :examples do
start_time { Kernel.rand(1441) }
end_time { Kernel.rand(1441) }
end
end
When I build this object and check if it's valid with RSpec I get errors relating to the start_time and end_time not being in the list. Checking the values of the object shows that they are properly in range.
I have tried the following to get it to work.
inclusion: { in: 0..1440 }
inclusion: [0..1440].flatten
---EDIT
In RSpec
let(:example_klass) { FactoryGirl.build(:example) }
expect(example_klass).to be_valid ##=> FAILS
In rails console
t = FactoryGirl.build(:example)
t.valid?
=> true
Any help would be appreciated!
Use
FactoryGirl.define do
sequence :start_time , (0..1441) do |n|
n
end
sequence :end_time , (0..1441) do |n|
n
end
end
and in your Example Factory use
FactoryGirl.define do
factory :examples do
start_time
end_time
end
end
Anything refer to the documentation: https://github.com/thoughtbot/factory_girl/wiki/Usage#sequences-and-associations .
Hope this helps
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.