luissimo
luissimo

Reputation: 978

10.1.4 michael hartl's ruby on rails tutorial TEST RED

I'm currently following the Ruby on Rails Tutorial from author Michael Hartl. And i got stuck at chapter 10.1.4 where the test should be GREEN but is currently RED, this is the error message:

Started with run options --seed 48963

 FAIL["test_valid_signup_information_with_account_activation", UsersSignupTest, 1.7533403700217605]
 test_valid_signup_information_with_account_activation#UsersSignupTest (1.75s)
        Failed assertion, no message given.
        test/integration/users_signup_test.rb:44:in `block in <class:UsersSignupTest>'

  41/41: [=========================================================================================================] 100% Time: 00:00:01, Time: 00:00:01

Finished in 1.78481s
41 tests, 173 assertions, 1 failures, 0 errors, 0 skips

This is the file containing the main error(FAIL), according to the report it should be 'assert user.reload.activated?':

require 'test_helper'

class UsersSignupTest < ActionDispatch::IntegrationTest

  def setup
    ActionMailer::Base.deliveries.clear
  end

  test "invalid signup information" do
    get signup_path
    assert_no_difference 'User.count' do
      post users_path, user: { name:  "",
                               email: "user@invalid",
                               password:              "foo",
                               password_confirmation: "bar" }
    end
    assert_template 'users/new'
    assert_select 'div#error_explanation'
    assert_select 'div.alert.alert-danger'
  end

  test "valid signup information with account activation" do
    get signup_path
    assert_difference 'User.count', 1 do
      post users_path, user: { name:  "Example User",
                               email: "[email protected]",
                               password:              "password",
                               password_confirmation: "password" }
    end
    assert_equal 1, ActionMailer::Base.deliveries.size
    user = assigns(:user)
    assert_not user.activated?
    # Try to log in before activation.
    log_in_as(user)
    assert_not is_logged_in?
    # Invalid activation token
    get edit_account_activation_path("invalid token")
    assert_not is_logged_in?
    get edit_account_activation_path(user.activation_token, email: 'wrong')
    assert_not is_logged_in?
    get edit_account_activation_path(user.activation_token, email: user.email)
    assert user.reload.activated?
    follow_redirect!
    assert_template 'users/show'
    assert is_logged_in?
  end
end

-

You can also check my repository on Git Repository

If anybody could help me with this issue it would be much appreciated and it would save me precious time.

Upvotes: 0

Views: 474

Answers (2)

Paul Fioravanti
Paul Fioravanti

Reputation: 16793

Your tests themselves look fine, but they would seem to be failing at the point when you're asserting that a user actually been activated once you have attempted to edit its account activation information:

get edit_account_activation_path(user.activation_token, email: user.email)
assert user.reload.activated? # <= test fails here

In the account_activations_controller.rb file in your repo, you have:

def edit
  user = User.find_by(email: params[:email])
  if user && !user.activated? && user.authenticated?(:activation, params[:id])
    user.activate # <= here's where you're activating the user
    log_in user
    flash[:success] = "Account activated!"
    redirect_to user
  else
    flash[:danger] = "Invalid activation link"
    redirect_to root_url
  end
end

Looking in your user.rb class, though, it would seem that you are actually missing the activate method entirely (it's a shame the error message doesn't seem to indicate this to you). So, add the following method to your User model and try running the specs again:

def activate
  update_columns(activated: true, activated_at: Time.current)
end

Edit

After checking out your repo at the point where your tests went red, I can see now that this issue is in your SessionsHelper#redirect_back_or method:

def redirect_back_or(default)
  redirect_to(session[:forwarding_url] || default)
  session.delete[:forwarding_url]
end

session.delete[:forwarding_url] should be session.delete(:forwarding_url), with parentheses instead of square brackets.

Upvotes: 0

tadman
tadman

Reputation: 211690

I think the problem boils down to these two lines:

assert_select 'div#<error_explanation>'
assert_select 'div.<alert alert-danger>'

These need to be valid CSS selectors as outlined in the error. They're not. A fixed version might be:

assert_select 'div#error_explanation'
assert_select 'div.alert.alert-danger'

It really depends on your styles. Remember these work just like jQuery, so they're analogous to $('div#error_explanation').

Upvotes: 1

Related Questions