System Tests

Episode #417 by Teacher's Avatar David Kimura

Summary

In this episode, we look at adding system tests in our application to test our Stimulus Controllers. We'll also look at how to DRY up some of the tests and how to run them in a headless environment.
rails tests stimulusjs 19:20

Chapters

  • Introduction (0:00)
  • Overview of the application (3:50)
  • Configuring system tests (4:46)
  • Hello World system test (5:41)
  • Taking screenshots (7:02)
  • Character Counter system test (7:26)
  • Tangent on Minitest vs rspec and Fixtures vs Factories (7:58)
  • Back to Character Counter system test (8:55)
  • User Authentications system test (10:18)
  • DRYing up the tests for more complex runs (13:33)
  • Final thoughts (18:33)

Resources

This episode is sponsored by Honeybadger

Download Source Code

Summary

# Terminal
bin/rails g system_test hello_world
bin/rails test:system

# test/application_system_test_case.rb
require "test_helper"

class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
  using = ENV["CI"] ? :headless_chrome : :chrome
  driven_by :selenium, using: using, screen_size: [1400, 1400]

  Dir[Rails.root.join("test", "system", "support", "*_helper.rb")].each do |file|
    require file
    module_name = File.basename(file, '.rb').camelize
    include ["System", "Support", module_name].join("::").constantize
  end
end

# test/system/hello_worlds_test.rb
require "application_system_test_case"

class HelloWorldsTest < ApplicationSystemTestCase
  # test "visiting the index" do
  #   visit hello_worlds_url
  #
  #   assert_selector "h1", text: "HelloWorld"
  # end

  test "should see the hello world" do
    visit root_url
    take_screenshot
    assert_text "Hello World"
  end
end

# test/system/character_counters_test.rb
require "application_system_test_case"

class CharacterCountersTest < ApplicationSystemTestCase
  test "should see the count down of characters" do
    visit root_url
    assert_text "There are 280 characters remaining."
    find("textarea").fill_in with: "This is a test message!"
    assert_text "There are 257 characters remaining."
  end

  test "authenticated users should see the count down of characters" do
    user = users(:one)
    login_as(user.email)
    visit root_url
    assert_text "There are 280 characters remaining."
    find("textarea").fill_in with: "This is a test message!"
    assert_text "There are 257 characters remaining."
  end
end

# test/system/user_authentications_test.rb
require "application_system_test_case"

class UserAuthenticationsTest < ApplicationSystemTestCase
  test "registers new user successfully" do
    visit new_user_registration_url
    fill_in "Email", with: "john.smith@example.com"
    fill_in "Password", with: "password"
    fill_in "Password confirmation", with: "password"
    click_on "Sign up"
    assert_text "Welcome! You have signed up successfully."
  end

  test "logs in successfully" do
    user = users(:one)
    visit root_url
    click_on "Sign In"
    fill_in "Email", with: user.email
    fill_in "Password", with: "password"
    click_on "Log in"
    assert_text "Signed in successfully."
  end
end

# test/fixtures/users.yml
one:
  email: one@example.com
  encrypted_password: <%= Devise::Encryptor.digest(User, 'password') %>