问题
The model User:
class User < ActiveRecord::Base
enum my_enum: [
:some_value1,
#....
]
Also I have a migration which adds my_enum
to User:
def change
add_column :users, :my_enum, :integer
end
And the fixture for FactoryGirl:
FactoryGirl.define do
factory :user do
email { Faker::Internet.email }
password { Faker::Internet.password(10) }
password_confirmation { password }
my_enum { nil }
end
end
Everything works fine. But when I run a test, I get an error:
Failure/Error: paid_user = FactoryGirl.create(:user)
ActiveModel::MissingAttributeError:
can't write unknown attribute `my_enum`
回答1:
It sounds like your test database has not been migrated properly. Try running the following command:
bundle exec rake db:migrate RAILS_ENV=test
.. and then try to run rspec again.
来源:https://stackoverflow.com/questions/27815020/rspec-cant-write-unknown-attribute-enum