This time I will write Rspec for the validation of Model
** ・ It is valid if you have name, email, password ** ** ・ It is invalid if there is no name, email, password **
I would like to test the above two points.
validates :name,
presence: true,
validates :email,
presence: true,
validates :password,
presence: true,
The test target is validation at the time of User registration. This time, I want to verify only the validity of validation, so I will use simple code as above.
require 'rails_helper'
Rspec.decribe User, type: model do
describe '#validates' do
context 'If your name, email address and password are correct' do
it 'Be valid' do
@user = User.new ({
name: "Test Taro",
email: "[email protected]",
password: "111111",
})
expect(@user).to be_valid
end
end
context 'If your name, email address, and password are incorrect' do
it 'To be validated' do
@user = User.new ({
name: "",
email: "",
password: "",
})
expect(@user.valid?).to be_falsy
end
end
end
end
Assign the correct and incorrect objects to the instance variables, respectively.
I use ** be_valid ** (predicut matcher) to test if validationg is valid, and ** be_falsy ** to make sure false is returned when it is not valid. ..
*** I want to see that the curly braces immediately after the new method when creating an object must be given to value and the parentheses must be given to each key. I personally got a little fit. ** **
2 examples, 0 failures
** You passed. ** **
I felt that Rspec to be validated was easy to understand because it could be described simply.
This time I implemented it all together, but I think that it is necessary to run the test for each column originally.
Also, when I hit the code, I thought that I used each parenthesis ** () {} [] ** with my own sense, so I felt that I needed to understand it again. I would like to write an article again.
ps→ It seems that Japanese eel fry come from Australia on the ocean current.
Recommended Posts