2

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`
6
  • Is the column available while you are using in your Rails console? Commented Jan 7, 2015 at 8:30
  • @ArupRakshit, shouldn't I reload a schema for the tests somehow? Commented Jan 7, 2015 at 8:33
  • Did you run rake db:test:prepare? Depending on which version of Rails you are using, this might not be necessary, but if you think the test schema is not up to date, maybe that's the issue. Commented Jan 7, 2015 at 8:48
  • 1
    I meant to test in Rails console in Test environment. :-) Commented Jan 7, 2015 at 9:02
  • @mrrogers, the tags say what version of Rails I'm using. Commented Jan 7, 2015 at 9:52

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm having a similar problem to OP and this did not solve it for me :-/
I am having the same problem, rspec is not letting me do a failing test for a bad enum type. It works fine for nil though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.