RSpec - can't write unknown attribute (enum)

2019-07-23 16:24发布

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条回答
啃猪蹄的小仙女
2楼-- · 2019-07-23 17:06

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.

查看更多
登录 后发表回答