Agile Web Development with Rails 8: Outdated Content and Mild Technical Critique (p. 223)

The 2nd bullet on p. 223 reads as follows (highlighting mine):

  • The system test in test/system/users_test.rb was generated by the scaffolding
    generator we used at the start of the chapter. Those tests don’t pass. See
    if you can get them to pass without breaking the other system tests. You’ll
    recall we created the module AuthenticationHelpers and included it in all of
    the system tests
    by default, so you might need to change the code to not
    do that so that you can properly test the login functionality.

This bullet is the only place the text “AuthenticationHelpers” appears in the Rails 8 version of the book. It looks as if there has not been a proper AuthenticationHelpers module since the version of the book on Rails 6.

Additionally, we did not technically include it in the system tests. We monkey-patched ActiveSupport::TestCase. The get and set methods are only defined in ActionDispatch::IntegrationTest. Consequently, login_as() works in controller tests but fails in system tests.

I wonder if an approach more like the one in the Rails 6 version of the book might serve us better:

module AuthenticationHelpers
  def login_as(user)
    if respond_to? :visit
      visit login_url
      fill_in :name, with: user.name
      fill_in :password, with: 'secret'
      click_on 'Login'
    else
      post login_url, params: { name: user.name, password: 'secret' }
    end
  end

  def logout
    delete logout_url
  end

  def setup
    login_as users(:one)
  end
end

class ActionDispatch::IntegrationTest
  include AuthenticationHelpers
end

class ActionDispatch::SystemTestCase
  include AuthenticationHelpers
end

Or perhaps ditch conditional logic and implement login_as() separately in each monkey-patch:

module AuthenticationHelpers
  def logout
    delete logout_url
  end

  def setup
    login_as users(:one)
  end
end

class ActionDispatch::IntegrationTest
  include AuthenticationHelpers

  def login_as(user)
    post login_url, params: { name: user.name, password: 'secret' }
  end
end

class ActionDispatch::SystemTestCase
  include AuthenticationHelpers

  def login_as(user)
    visit login_url
    fill_in :name, with: user.name
    fill_in :password, with: 'secret'
    click_on 'Login'
  end
end

Just some thoughts.