RSpec - can't write unknown attribute (enum)

对着背影说爱祢 提交于 2020-01-04 17:32:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!