When I validated a custom message using shoulda-matchers, I didn't know how to write it, so I'm a little stuck, so I'll share it here.
shoulda-matchers are gems that make it easier to run long tests when written in regular RSpec.
For example, suppose you have a model with the following validations.
user.rb
class User < ApplicationRecord
validates :nickname, presence: true, length: { maximum: 30 }
end
This test runs in one line as follows:
spec/models/user_spec.rb
it { is_expected.to validate_length_of(:nickname).is_at_most(30) }
It's quite convenient.
For example, suppose you set up a validation with your own error message, like this:
user.rb
validates :email, presence: { message: 'Is not included' }
If you want to check if your own error message is displayed at the same time as validation
spec/models/user_spec.rb
it { should validate_presence_of(:email). with_message('Is not included') }
By writing like this, you can check your own error message and validation at the same time.
There are many other ways to use it, so if you are interested, please read this. The introduction method is also written. Shoulda-Matchers READ ME
I output what I learned every day! If you have any suggestions, I would appreciate it if you could comment!
Recommended Posts