Nick
Nick

Reputation: 3090

Error message after implementing has_secure_password

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

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

  def setup
    @user = User.new(email: "[email protected]",
                     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?

Upvotes: 2

Views: 256

Answers (1)

BroiSatse
BroiSatse

Reputation: 44715

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.

Upvotes: 2

Related Questions