Basic Testing Introduction in Rails

#100 Basic Testing Introduction in Rails
10/8/2017

Summary

Using the Rails 5.1.X defaults, we have a look at what is configured and explore the different types of tests; using the provided MiniTest and Capybara.
8
rails test 13:29 min

Summary

Terminal# Runs tests
rails test

# Runs system tests
rails test:system
users_controller_test.rbrequire 'test_helper'

class UsersControllerTest < ActionDispatch::IntegrationTest
  setup do
    @user = users(:one)
  end

  test "should get index" do
    get users_url
    assert_response :success
  end

  test "should get new" do
    get new_user_url
    assert_response :success
  end

  test "should create user" do
    assert_difference('User.count') do
      post users_url, params: { user: { email: @user.email, first_name: @user.first_name, last_name: @user.last_name } }
    end

    assert_redirected_to user_url(User.last)
  end

  test "should show user" do
    get user_url(@user)
    assert_response :success
  end

  test "should get edit" do
    get edit_user_url(@user)
    assert_response :success
  end

  test "should update user" do
    patch user_url(@user), params: { user: { email: @user.email, first_name: @user.first_name, last_name: @user.last_name } }
    assert_redirected_to user_url(@user)
  end

  test "should destroy user" do
    assert_difference('User.count', -1) do
      delete user_url(@user)
    end

    assert_redirected_to users_url
  end
end
user_test.rbrequire 'test_helper'

class UserTest < ActiveSupport::TestCase
  test "email validation should trigger" do
    assert_not User.new(first_name: 'First Name', last_name: 'Last Name').save
  end

  test "user should save" do
    assert User.new(first_name: 'First Name', last_name: 'Last Name', email: '[email protected]').save
  end
end
users_test.rbrequire "application_system_test_case"

class UsersTest < ApplicationSystemTestCase
  test "visiting the index" do
    visit users_url
    assert_selector "h1", text: "User"
  end

  test "should create user" do
    visit new_user_url
    fill_in 'First name', with: 'first name'
    fill_in 'Last name', with: 'last name'
    fill_in 'Email', with: '[email protected]'
    click_button 'Create User'

    visit users_url
    within 'table' do
      assert_selector 'tr td', text: 'first name'
      assert_selector 'tr td', text: 'last name'
      assert_selector 'tr td', text: '[email protected]'
    end
  end
end
user.rbclass User < ApplicationRecord
  validates :email, presence: true
end
users.ymlone:
  first_name: MyString
  last_name: MyString
  email: MyString

two:
  first_name: MyString
  last_name: MyString
  email: MyString



2031640?v=3&s=64
3lackRos3 said 13 days ago:

Congrats on your 100th video.  Keep up the good work.

635114?v=3&s=64
kobaltz said 13 days ago:

Thank you! It has been a long time coming.

672989?v=3&s=64
sadiqmmm said 13 days ago:

Amazing! Thanks for all the videos you have created. Congratulations for the 100th video :)

00000000000000000000000000000000?d=mm&f=y&s=64
[email protected] said 13 days ago:

Congratulations for the 100th video \o/

Photo
Sam Van Damme said 13 days ago:

Congrats! You made me a lot smarter :)

I would love more testing tutorials.

Login to Comment