2

I have developed some tests in Rails that work fine. Then I added:

  • In model file: has_secure_password
  • In Gem file: gem 'bcrypt', '3.1.7' (after bundle install it shows up in Gemfile.lock)
  • I added password_digest as string to the migration file (and after migrating it indeed shows up in schema.rb)

And I added password and password_confirmation to setup in the test file:

  def setup
    @user = User.new(email: "user@example.com",
                     username: "example user",
                     firstname: "Allan",
                     location: "Europe",
                     password: "foobar", 
                     password_confirmation: "foobar")
  end

Now when I run rake test I get errors saying:

NoMethodError: undefined method 'password_digest=' for #<User:0x00000002f9c020> test/models/user_test.rb:6:in 'setup'.

Line 6 refers to the line @user = User.new ...

So it seemed to have implemented the gem and additional column correctly, and yet I get this error message. Does anyone have an idea what could be the cause?

Nick
  • 3,496
  • 7
  • 42
  • 96
  • 1
    Have you created new migration or did you modify the existing migration? – BroiSatse Apr 10 '15 at 22:34
  • I first ran `bundle exec rake db:migrate VERSION=0` and then `bundle exec rake db:migrate` but this didn't help. – Nick Apr 10 '15 at 22:42
  • 1
    It doesn't really answer my question, so I assume you changed the existing migration. :) This will cause your test database to be out of sync with your development database. Run `rake db:test:prepare` - it will load your current schema.rb into your test database. – BroiSatse Apr 10 '15 at 22:49
  • That solved it! Don't yet know why that is and why what I did wasn't sufficient, but I'm gonna do some research on what `rake db:test:prepare` does exactly. – Nick Apr 10 '15 at 22:52
  • Please add it as the answer – Nick Apr 10 '15 at 22:52

1 Answers1

2

It seems you have modified and rerun your existing migration. In that case, your test database is out of sync with your dev database. You need to load your schema into the test database with:

rake db:test:prepare

Explanation:

In rails development you have two completely separate environments - test and development. Development is the environment you use to view what you have written - it is a default for rails s and rails c. Test environment is used only for testing. Those environments have two separate databases.

Until recently, every time you created migration you had to run it twice, once per each environment (so both databases are in sync) or at least load the database schema to test database. Newest rails version is slightly smarter - before it runs tests it will check if all migrations has been run and then if the version of test database matches the dev database. If not, it will update test database from the schema.

Since you rerun existing migration, both databases yielded the same version, so your test database has not been updated automatically, hence the need to update it manually.

BroiSatse
  • 44,031
  • 8
  • 61
  • 86