jeudi 4 octobre 2018

Michael Hartl Ruby on Rails tutorial Chapter 9 - 10 testing errors

I've been following the railstutorial.org tutorial online but I keep getting these two errors when testing in chapter 10.

# Running:

.........F
Failure:
UsersLoginTest#test_login_without_remembering 
[/project/test/integration/users_login_test.rb:50]:
Expected "d-BD3_B8srKXUkp6HvXMlg" to be empty.


bin/rails test test/integration/users_login_test.rb:45

..............F
Failure:
UsersIndexTest#test_index_as_admin_including_pagination_and_delete_links [/project/test/integration/users_index_test.rb:17]:
<Mario> expected but was
<delete>..
Expected 0 to be >= 1.


bin/rails test test/integration/users_index_test.rb:10


Finished in 4.259130s, 10.0960 runs/s, 23.9486 assertions/s.
43 runs, 102 assertions, 2 failures, 0 errors, 0 skips

I've double-checked the tutorial but keep getting the same error messages. Here's my code:

users_index_test.rb Online 17 I'm referencing the first_name only because I couldn't get the first and last name at the same time. It would be helpful if anybody knows I can do that.

require 'test_helper'

class UsersIndexTest < ActionDispatch::IntegrationTest

  def setup
    @admin     = users(:max)
    @non_admin = users(:mario)
  end

  test "index as admin including pagination and delete links" do
    log_in_as(@admin)
    get users_path
    assert_template 'users/index'
    assert_select 'div.pagination'
    first_page_of_users = User.paginate(page: 1)
    first_page_of_users.each do |user|
      assert_select 'a[href=?]', user_path(user), text: user.first_name
      unless user == @admin
        assert_select 'a[href=?]', user_path(user), text: 'delete'
      end
    end
    assert_difference 'User.count', -1 do
      delete user_path(@non_admin)
    end
  end

  test "index as non-admin" do
    log_in_as(@non_admin)
    get users_path
    assert_select 'a', text: 'delete', count: 0
  end
end

users_login_test.rb Line 50 on this test file doesn't seem to have any errors in my opinion.

require 'test_helper'

class UsersLoginTest < ActionDispatch::IntegrationTest

  def setup
    @user = users(:max)
  end

  test "login with invalid information" do
    get login_path
    assert_template 'sessions/new'
    post login_path, params: { session: { email: "", password: "" } }
    assert_template 'sessions/new'
    assert_not flash.empty?
    get root_path
    assert flash.empty?
  end

  test "login with valid information followed by logout" do
    get login_path
    post login_path, params: { session: { email: @user.email, password: 'password' } }
    assert is_logged_in?
    assert_redirected_to @user
    follow_redirect!
    assert_template 'users/show'
    assert_select "a[href=?]", login_path, count: 0
    assert_select "a[href=?]", logout_path
    assert_select "a[href=?]", user_path(@user)
    delete logout_path
    assert_not is_logged_in?
    assert_redirected_to root_url
    # Simulate a user clicking logout in a second window
    delete logout_path
    follow_redirect!
    assert_select "a[href=?]", login_path
    assert_select "a[href=?]", logout_path,      count: 0
    assert_select "a[href=?]", user_path(@user), count: 0
  end

  test "login with remembering" do
    log_in_as(@user, remember_me: '1')
    assert_not_empty cookies['remember_token']
  end

  test "login without remembering" do
    # Log in to set the cookie.
    log_in_as(@user, remember_me: '1')
    # Log in again and verify that the cookie is deleted.
    log_in_as(@user, remember_me: '0')
    assert_empty cookies['remember_token']
  end
end

Just in case this helps, I have attached the seeds.rb file and users.yml file:

seeds.rb

User.create!(first_name: "Max",
             last_name: "Rahim",
             email: "mrahim@email.com",
             password: "DietCoke",
             password_confirmation: "DietCoke",
             admin: true)

99.times do |n|
  first_name = Faker::Name.first_name
  last_name = Faker::Name.last_name
  email = "example-#{n+1}@email.com"
  password = "password"
  User.create!(first_name: first_name,
               last_name: last_name,
               email: email,
               password: password,
               password_confirmation: password)
 end

users.yml This is the test fixtures from Listing 10.47: Adding 30 extra users to the fixture. I'm not sure but I think this is the part that might be causing the issue. Any help will be welcomed.

max:
  first_name: Max
  last_name: Rahim
  email: mrahim@ymail.com
  password_digest: <%= User.digest('password') %>
  admin: true

mario:
  first_name: Mario
  last_name: Rahim
  email: mrahim@email.com
  password_digest: <%= User.digest('password') %>

erica:
  first_name: Erica
  last_name: Rahim
  email: erahim@email.com
  password_digest: <%= User.digest('password') %>

matt:
  first_name: Matt
  last_name: Rahim
  email: mattrahim@email.com
  password_digest: <%= User.digest('password') %>

<% 30.times do |n| %>
user_<%= n %>:
  first_name:  <%= "User #{n}" %>
  last_name:  <%= "User #{n}" %>
  email: <%= "user-#{n}@example.com" %>
  password_digest: <%= User.digest('password') %>
<% end %>

Aucun commentaire:

Enregistrer un commentaire