[RUBY] Rails tutorial test

Introduction

As you learn the Rails tutorial, Chapter 3 describes how important "automation testing" is. It seems that there is a lot of controversy about how to write a test, but in this article I have summarized what I think it is better to suppress the test in my own way.

About the test framework

There are several types of test frameworks, each of which has its own characteristics such as how to write code. Here, we will introduce the two main types of frameworks.

I used it as a reference → 6 popular Rails test frameworks! Which would you use, RSpec or Minitest?

Minitest -Rails' default framework. ・ Learning cost is relatively low. -It is also used in the Rails tutorial. -It is said that the processing speed is relatively fast. -The default function is the minimum required. ・ Small amount of code, simple

Rspec ・ Written in natural language by people close to each other. ・ High utilization rate. ・ It takes time to get used to it. -It is said that the processing speed is slower than Minitest. -Abundant default functions. ・ The amount of code is large and complicated.

Both seem to have advantages and disadvantages. It's best for beginners to start with Minitest and then learn Rspec.

About test types

Depending on the reference literature, there may be two types or more, but this time we will introduce two types of tests.

I used it as a reference → Rails Testing Guide [Software Testing-wilki](https://ja.wikipedia.org/wiki/%E3%82%BD%E3%83%95%E3%83%88%E3%82%A6%E3%82%A7% E3% 82% A2% E3% 83% 86% E3% 82% B9% E3% 83% 88)

Unit test

Tests performed in small units such as functions and methods. Check the behavior of the model and view helper alone.

Example:

test/helpers/application_helper_test


  test "full title helper" do
    assert_equal full_title,         "Ruby on Rails Tutorial Sample App"
  end

An assert is code that evaluates an object or expression and checks to see if it gives the expected result (true). The above verifies that assert_equal has a full_title that exactly matches the "Ruby on Rails Tutorial Sample App".

test/models/user_test.rb


  test "name should not be too long" do
    @user.name = "a" * 51
    assert_not @user.valid?
  end

assert_not is the code that checks if false is obtained as opposed to assert. The above verifies that the user is not valid if the user's name is 51 characters.

Integration test

A test that combines programs. Check whether the individual functions are linked correctly, assuming the actual operation of the user.

Example:

test/integration/users_login_test


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

The above is testing for users who have entered a password that is not valid.

  1. Generate a login page (login_path) with the get method.   →get login_path

  2. Verify that the login page template is selected.   →assert_template 'sessions/new'

  3. Post a valid email address and invalid password to login_path.   →post login_path,params: {session: { email: @user.email,password: "invalid"}}

  4. Verify that the user is not logged in.   →assert_not is_logged_in?

The is_logged_in? method is defined by:

test/test_helper.rb


  #Returns true if the test user is logged in
  def is_logged_in?
    !session[:user_id].nil?
  end
  1. Verify that the login page template is selected.   →assert_template 'sessions/new'

  2. Verify that the error message is displayed.   →assert_not flash.empty?

  3. Create a home page (root_path) with the get method.   →get root_path

  4. Verify that no error message is displayed.   →assert flash.empty?

It has become a flow.

Summary

To be honest, I'm not sure if I'll test it myself after finishing the Rails tutorial. Also, it seems that you will be asked how rigorously the test will be done. I thought it was important to get used to the numbers.

Recommended Posts

Rails tutorial test
rails tutorial
rails tutorial
rails tutorial
rails tutorial
rails tutorial
rails tutorial
rails tutorial
rails tutorial Chapter 1
Rails tutorial memorandum 1
Rails tutorial memorandum 2
rails tutorial Chapter 7
rails tutorial Chapter 5
rails tutorial Chapter 10
rails tutorial Chapter 9
rails tutorial Chapter 8
Start Rails Tutorial
[Beginner] Rails Tutorial
Rails Tutorial Chapter 5 Notes
Rails Tutorial Chapter 10 Notes
Rails Tutorial Chapter 3 Notes
Rails Tutorial cheat sheet
Test run on rails
Rails Tutorial Chapter 3 Learning
[Rails] Learning with Rails tutorial
Rails Tutorial Memorandum (Chapter 3, 3.1)
[Rails] Test with RSpec
Rails Tutorial Chapter 4 Notes
Rails Tutorial Chapter 4 Learning
Rails Tutorial Chapter 1 Learning
Rails Tutorial Chapter 2 Learning
Rails Tutorial Chapter 8 Notes
rails tutorial fighting notes Ⅲ
Rails Tutorial Memorandum (Chapter 3, 3.3.2)
I rewrote the Rails tutorial test with RSpec
test
[Ruby on Rails Tutorial] Error in the test in Chapter 3
11.1 AccountActivations Resources: Rails Tutorial Notes-Chapter 11
Rails Tutorial Records and Memorandum # 0
test
[Rails] Test code using Rspec
test
Rails Tutorial (4th Edition) Summary
test
[Rails Tutorial Chapter 4] Rails-flavored Ruby
[Rails] About Rspec response test
Resolve ActiveRecord :: NoDatabaseError when doing rails test (Rails tutorial Chapter 3)
[Rails] Implementation of tutorial function
Rails Tutorial Chapter 14 Creating Relationship Test Data with Factory Bot
[Rails Struggle/Rails Tutorial] Summary of Rails Tutorial Chapter 2
Piped together grep ?: Rails Tutorial Notes--Chapter 8
[Rails Tutorial Chapter 5] Create a layout
rails tutorial chapter 10 summary (for self-learning)
Chewing Rails Tutorial [Chapter 2 Toy Application]
[Rails5] Rspec -Unit test when nesting-
Rails Tutorial (4th Edition) Memo Chapter 6
Let's unit test with [rails] Rspec!
[Rails g.error]
Rails Tutorial 6th Edition Learning Summary Chapter 10
Rails Tutorial 6th Edition Learning Summary Chapter 7
Rails basics