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.
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.
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)
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.
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.
Generate a login page (login_path) with the get method. →get login_path
Verify that the login page template is selected. →assert_template 'sessions/new'
Post a valid email address and invalid password to login_path. →post login_path,params: {session: { email: @user.email,password: "invalid"}}
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
Verify that the login page template is selected. →assert_template 'sessions/new'
Verify that the error message is displayed. →assert_not flash.empty?
Create a home page (root_path) with the get method. →get root_path
Verify that no error message is displayed. →assert flash.empty?
It has become a flow.
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