[RUBY] [RSpec] How to test error messages set by Shoulda-Matchers

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.

What are shoulda-matchers?

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.

If you want to test your own error message

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

Thank you for reading to the end!

I output what I learned every day! If you have any suggestions, I would appreciate it if you could comment!

Recommended Posts

[RSpec] How to test error messages set by Shoulda-Matchers
[RSpec] How to write test code
[RSpec on Rails] How to write test code for beginners by beginners
How to write an RSpec controller test
How to display error messages in Japanese
How to use "sign_in" in integration test (RSpec)
[Rails] How to get success and error messages
Introduction to RSpec 1. Test, RSpec
Set RSpec to DRY
How to set Docker nginx
How to set Java constants
How to test a private method with RSpec for yourself
How to erase test image after running Rspec test with CarrierWave
[Rails] How to display error messages for comment function (for beginners)
[Rails / RSpec] How to deal with element has zero size error
How to unit test Spring AOP
[Rails] How to use flash messages
How to set Spring Boot + PostgreSQL
How to display error messages and success messages when registering as a user
How to set different source / target versions for production code and test code
How to prevent duplicate processing by addEventListener
I want to eliminate duplicate error messages
How to filter JUnit Test in Gradle
How to put out the error bundling
[Note] How to get started with Rspec
How to set up and use kapt
[Java] How to set the Date time to 00:00:00
How to test private scope with JUnit
Needed for iOS 14? How to set NSUserTrackingUsageDescription
[SpringBoot] How to write a controller test
[Rails] How to decide the destination by "rails routes"
How to find the cause of the Ruby error
[Java] How to test for null with JUnit
I want to test Action Cable with RSpec test
The road to Japaneseizing Rails devise error messages
How to test interrupts during Thread.sleep with JUnit
How to output CSV created by Rails to S3
JUnit 5: How to write test cases in enum
[Java] (for MacOS) How to set the classpath
How to separate .scss by controller in Rails
How to set up and operate jEnv (Mac)
Introduction of Rspec and Japanese localization of error messages
How to set up JavaED Full Edition (pleiades)
How to write test code with Basic authentication
How to utilize knowledge to reduce support work by 10 to 20%
Spring Boot --How to set session timeout time
Rspec: I want to test the post-execution state when I set a method on subject
[Rails] How to solve the error "undefined method` visit'" when using Capybara with Rspec