[Rails] Test code using Rspec

Introduction

I'm going to introduce Rspec and write test code. The command uses the command of the docker environment.

Introducing Gem

group :development, :test do
  gem 'rspec-rails'
  gem 'factory_bot_rails'
  gem 'faker', "~> 2.8"
end

docker-compose run web bundle install docker-compose build Installation is complete.

Rspec settings

docker-compose run webrails g rspec:install When the above command is executed, the following file is generated.

create  .rspec
create  spec
create  spec/spec_helper.rb
create  spec/rails_helper.rb
.rspc
--format documentation

Code creation

spec/models/user_spec.rb


require 'rails_helper'

describe User do
  describe '#create' do

    it "username and email,password and password_Being able to register if confirmation exists" do
      user = build(:user)
      expect(user).to be_valid
    end

    it  "Cannot register without username" do 
      user = build(:user, username: nil)
      user.valid?
      expect(user.errors[:username]).to include("can't be blank")
    end

    it  "Cannot register without email" do 
      user = build(:user, email: nil)
      user.valid?
      expect(user.errors[:email]).to include("can't be blank")
    end

    it  "Cannot register without password" do 
      user = build(:user, password: nil)
      user.valid?
      expect(user.errors[:password]).to include("can't be blank")
    end

    it  "password even if password exists_Cannot register without confirmation" do 
      user = build(:user, password_confirmation: "")
      user.valid?
      expect(user.errors[:password_confirmation]).to include("doesn't match Password")
    end

    it  "Cannot register if there are duplicate emails" do
      user = create(:user)
      another_user = build(:user, email: user.email)
      another_user.valid?
      expect(another_user.errors[:email]).to include("has already been taken")
    end

    it  "If the password is 6 characters or more, you can register" do
      user = build(:user, password: "000000", password_confirmation: "000000")
      expect(user).to be_valid
    end

    it  "Cannot register if password is 5 characters or less" do
      user = build(:user, password: "00000", password_confirmation: "00000")
      user.valid?
      expect(user.errors[:password]).to include("is too short (minimum is 6 characters)")
    end:grin:

  end
end

spec/factories/users.rb


FactoryBot.define do
  factory :user do
    username                 {"aaa"}
    password                 {"000000"}
    password_confirmation    {"000000"}
    sequence(:email)     {Faker::Internet.email}
  end
end

docker-compose run web bundle exec rspec

8 examples, 0 failures

If so, the test has passed successfully.

in conclusion

This time, I entered the test using Rspec. We will also test the controller in the future. Thank you for reading to the end: grin:

Recommended Posts

[Rails] Test code using Rspec
RSpec test code execution
[Rails] Test with RSpec
[Rails] About Rspec response test
[Rails] Test of star evaluation function using Raty [Rspec]
[Rails / RSpec] Write Model test (using Shoulda Matchers / FactoryBot)
Image upload using CarrierWave ~ Rspec test ~
[Rails5] Rspec -Unit test when nesting-
[RSpec] How to write test code
[RSpec] Unit test (using gem: factory_bot)
Let's unit test with [rails] Rspec!
I'm writing test code using RSpec, but it doesn't work
[Rails] Unit test code for User model
[Ruby on Rails] View test with RSpec
[Ruby on Rails] Code check using Rubocop-airbnb
Try using the Rails API (zip code)
[RSpec on Rails] How to write test code for beginners by beginners
Understand code coverage with Rspec, the Ruby on Rails test framework
[Ruby on Rails] Controller test with RSpec
Efficient test code
Rspec Basics [Rails]
[Ruby on Rails] Model test with RSpec
[Rails5] Rspec -validation-
About RSpec (Rails)
I rewrote the Rails tutorial test with RSpec
Introducing Rspec, a Ruby on Rails test framework
Test code using mock with JUnit (EasyMock center)
Introduction to RSpec 1. Test, RSpec
Test run on rails
[Test code learning / output]
[Ruby On Rails] Error in test using RSpec MySQL client is not connected
Test Nokogiri with Rspec.
Rails, RSpec installation procedure
[Rails] From test preparation to model unit testing [RSpec]
RSpec unit test code creation procedure (login user creation Ver.)
Rails5 Rspec test error ArgumentError: Factory not registered: user
Unit test code for a model using RSpec, which has a little peculiarity ~ User registration
I tried unit testing Rails app using RSpec and FactoryBot
Try using view_component with rails
SNS authentication using Rails google
[RSpec] Use WebMock to test logic using an external API
What I was addicted to while using rspec on rails
[RSpec] Let's use FactoryBot [Rails]
Japaneseize using i18n with Rails
[Rails] Japanese localization using rails-i18n
Test Active Strage with RSpec
Java test code method collection
Unit test architecture using ArchUnit
Organize Rails routing using draw
Test GraphQL resolver with rspec
Implementation of unit test code
Ajax bookmark function using Rails
[Rails] Integration test using Capybara (from introduction to simple test execution)
Error when using rails capybara
[Rails] Try using Faraday middleware
Detailed tips when using Rails
[Rails 6] Star-shaped review using Raty.js
[Challenge CircleCI from 0] Build an automated test (Rails6.0, mysql8.0, Rspec)
Password dummy data generation notes in Rails model unit test code
[Hidden_field] Let's send information using rails hidden_field !!!!
Rails 5 Code Reading Part 2 ~ Action View ~