Test run on rails

Quote Rails Tutorial

What is a test?

The word is to make sure that the feature is implemented correctly. It's also a safety net when it comes to writing tests, and it's itself an "executable document" of your application's source code. With all the tests in place, you don't have to spend extra time chasing bugs, and if done right, it's definitely faster than without tests.

Benefits of testing

  1. You can prevent regression bugs that may cause a malfunction.
  2. You can safely refactor (improve) your code.
  3. The test code acts as a client from the perspective of the application code, which is also useful when designing an application and deciding how to interface with other parts of the system.

Run the test

The test file is created when rails generate controller is executed in the terminal.

Terminal


#Example
$ rails generate controller StaticPages home help

test/controllers/static_pages_controller_test.rb


require 'test_helper'

class StaticPagesControllerTest < ActionDispatch::IntegrationTest

  test "should get home"do ← home page test
    get static_pages_home_url ← Send GET request to home action
    assert_response :success ← The response to the request should be [Success]
  end

  test "should get help" do
    get static_pages_help_url
    assert_response :success
  end
end

Terminal


$ rails db:migrate     #Required by some systems
$ rails test
2 tests, 2 assertions, 0 failures, 0 errors, 0 skips

The code is implemented without any problem because of 0 errors.

I try to make an error on purpose

Assuming you added the about page, try adding the code.

test/controllers/static_pages_controller_test.rb


require 'test_helper'

class StaticPagesControllerTest < ActionDispatch::IntegrationTest

  test "should get home" do
    get static_pages_home_url
    assert_response :success
  end

  test "should get help" do
    get static_pages_help_url
    assert_response :success
  end

  test "should get about" do
    get static_pages_about_url
    assert_response :success
  end
end

Test run

Terminal


$ rails test

NameError: undefined local variable or method `static_pages_about_url'
3 tests, 2 assertions, 0 failures, 1 errors, 0 skips

Error message if the URL to the About page cannot be found. Try modifying the routing file.

config/routes.rb


Rails.application.routes.draw do
  get  'static_pages/home'
  get  'static_pages/help'
  get  'static_pages/about'← Add
  root 'application#hello'
end

Test run

Terminal


$ rails test

AbstractController::ActionNotFound:
The action 'about' could not be found for StaticPagesController

Error message if there is no about action on the StaticPages controller. Try adding the about action.

app/controllers/static_pages_controller.rb


class StaticPagesController < ApplicationController

  def home
  end

  def help
  end

def about ← added
end ← added
end

Test run

Terminal


$ rails test
ActionController::UnknownFormat: StaticPagesController#about
is missing a template for this request format and variant.

Error message if there is no template (view). Create a view file with the following command or right-click.

Terminal


$ touch app/views/static_pages/about.html.erb

Test run

Terminal


$ rails test
3 tests, 3 assertions, 0 failures, 0 errors, 0 skips

The test is complete because it became 0 errors! !!

Refactor your code now that the tests are complete (This time it's done because there is no code to refactor)

This is called the "red / green / REFACTOR" cycle.


Recommended Posts

Test run on rails
Rails tutorial test
Tailwind on Rails
[Ruby on Rails] View test with RSpec
[Ruby on Rails] Controller test with RSpec
[Ruby on Rails] Model test with RSpec
Run STS4 on Mac
Launch Rails on EC2
Run PostgreSQL on Java
Deploy RAILS on EC2
Introducing Rspec, a Ruby on Rails test framework
Ruby on Rails Elementary
Ruby on Rails basics
[Rails] Test with RSpec
Yay! I'm on Rails!
Ruby On Rails Association
Run Processing on Ant
Run tiscamera on Ubuntu 18.04
Run phpunit on Docker
[Ruby on Rails Tutorial] Error in the test in Chapter 3
Run Ruby on Rails RSpec tests with GitHub Actions
Resolve ActiveRecord :: NoDatabaseError on rails6
rails test db only drop
Ruby on rails learning record -2020.10.03
Run VS Code on Docker
Install Sidekiq pro on Rails
Ruby on rails learning record -2020.10.04
[Ruby on Rails] Debug (binding.pry)
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby on Rails config configuration
Run openvpn on Docker (windows)
Ruby on Rails basic learning ①
[Ruby on Rails] about has_secure_password
Ruby on rails learning record-2020.10.07 ②
Run chromium-mir-kiosk on Ubuntu Core
Run Rails whenever with docker
[Rails] Test code using Rspec
Yay! I'm on Rails! Repetition
Run java applet on ubuntu
Install Rails on macOS Catalina
Commentary on partial! --Ruby on Rails
Post a video on rails
Ruby on rails learning record-2020.10.07 ①
Run Eclipse CDT on Ubuntu
Cancel Ruby on Rails migration
[Rails] About Rspec response test
Ruby on rails learning record -2020.10.06
Run mruby / c on PSoC 5
Ruby on Rails validation summary
Ruby on Rails Basic Memorandum
How to run React and Rails on the same server
Run Java EE applications on CICS
Ruby on Rails Overview (Beginner Summary)
test
How to deploy jQuery on Rails
[Ruby on Rails] Read try (: [],: key)
Run SSE (Server-Sent-Event) samples on docker
[Ruby on Rails] yarn install --check-files
Ruby on Rails variable, constant summary
Steps to run docker on Mac