RSpec - Cannot Write Unknown Attribute (Enumeration)

Model user:

class User < ActiveRecord::Base

  enum my_enum: [
    :some_value1,
    #....
  ] 

      

Also I have a transition that adds my_enum

to the user:

def change
  add_column :users, :my_enum, :integer
end

      

And attachment 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 perfectly. But when I run the test, I get the error:

Failure/Error: paid_user = FactoryGirl.create(:user)
     ActiveModel::MissingAttributeError:
       can't write unknown attribute `my_enum`

      

+3


source to share


1 answer


It looks like your test database was not migrated correctly. Try the following command:

bundle exec rake db:migrate RAILS_ENV=test

      



.. and then try running rspec again.

+5


source







All Articles