[RUBY] [Rails5] Rspec -validation-


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.

1. Test target

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.

2. Implementation

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

    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

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. ** **

3. Test result

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

[Rails5] Rspec -validation-
Rspec Basics [Rails]
About RSpec (Rails)
[Rails 6] Validation by belongs_to
[Rails] Customize devise validation
[Rails] Test with RSpec
Rails, RSpec installation procedure
[Rails] Japanese longest surname (validation)
[RSpec] Let's use FactoryBot [Rails]
How to write Rails validation
[Rails] How to use validation
[Rails] About Rspec response test
[Rails] Unexpected validation error in devise
Rails book review app RSpec introduction
RSpec ~ Task model validation test creation
[Rails] Validation settings and Japanese localization
[Rails5] Rspec -Unit test when nesting-
Let's unit test with [rails] Rspec!
[Rails g.error]
Rails basics
RSpec setup
Rails API
Rails migration
[Rails] first_or_initialize
[Ruby on Rails] View test with RSpec
Introducing RSpec
rails tutorial
RSpec-Testing'users validation'
About Rails 6
Rails foundation
Rails memorandum
[Rails] Use validation on a specific controller
RSpec preparation
rails tutorial
Install RSpec
rails tutorial
[Ruby on Rails] Controller test with RSpec
RSpec Setup
RSpec Basics
Rspec, TDD (1)
[Rails] devise
rails tutorial
rails tutorial
[Rails] Implementation of validation that maintains uniqueness
Rails Tips
rails method
rails tutorial
Rails validation and null: false Personal notes
[Rails] ActiveRecord
[Rails] form_with
Rails Review 2
Validation settings for Ruby on Rails login function
I rewrote the Rails tutorial test with RSpec
[Ruby on Rails] Until the introduction of RSpec
Rails: Japanese localization of validation messages including devise
Introducing Rspec with Ruby on Rails x Docker
[Rails] Japanese localization of validation error message ~ ja.yml ~
Introducing Rspec, a Ruby on Rails test framework
[Validation] rails How to specify after today's date
Rails beginners tried to get started with RSpec