I have a Model that has an enum as attribute.
class ApplicationLetter < ActiveRecord::Base
belongs_to :user
belongs_to :event
validates :user, :event, presence: true
enum status: {accepted: 1, rejected: 0, pending: 2}
end
As well as a factory that generates this model and sets a value for the enum
FactoryGirl.define do
factory :application_letter do
motivation "motivation"
user
event
status :accepted
end
end
In a controller test I want to get valid attributes through the factory
let(:valid_attributes) { FactoryGirl.build(:application_letter).attributes }
and create a application with these attributes.
application = ApplicationLetter.create! valid_attributes
But I get the following error:
ArgumentError: '1' is not a valid status
Why is status interpreted as string? If I change the status in the factory I get the same error, but with the right corresponding number.
you can do it more dynamically:
FactoryGirl.define do
factory :application_letter do
motivation "motivation"
user
event
status { ApplicationLetter.statuses.values.sample }
end
end
in this each time you will get different status
OR if wanna use static value you have to use integer, because enum
s by default use integer values
All you need in your factory is status 'accepted'
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With