[RUBY] Let's unit test with [rails] Rspec!

There are two types of tests: unit tests and integration tests.

-Unit test A test to see if it works properly for one program. (Example) For each model class

-Integration testing 1 Test for lotus processing. (Example) Flow from inputting and sending a value from the user's new registration screen until the record is added to the database

Introduction of "Rspec" and "factory_bot"

"Rspec" is a jem for testing, "Factory_bot" creates information temporarily when testing It is a help tool.

Therefore, let's install them all at once.

①jemno Installation

Gemfile


group :development, :test do
  gem 'rspec-rails'
  gem 'factory_bot_rails'
end

Terminal


bundle install

(2) Install the minimum required file / directory structure of RSpec in Rails

Terminal


$ rails g rspec:install
#>      create  .rspec                  #RSpec configuration file
#>      create  spec                    #Store specs
#>      create  spec/spec_helper.rb     #Helper for spec description
#>      create  spec/rails_helper.rb    #Helper for Rails-specific spec description
specフォルダ.png

Add the required directories and files here.

③ Create necessary files Here is a validation test for the users model I will go.

◆ Test file The files in spec / are 1 for the rb file under test under app / Arrange in a one-to-one correspondence.

The specs for app / models / user.rb It will be spec / models / user_spec.rb.

spec-file.png

◆ Data file for testing (factory_bot) By placing the factory in spec / factories You can easily use the test data.

factories.png

④ File description

Set so that the namespace can be omitted.

spec/rails_helper.rb


RSpec.configure do |config|
+  config.include FactoryGirl::Syntax::Methods
end

.rspec


--format documentation
--require spec_helper

spec/models/user_spec.rb



spec/factories/users.rb


FactoryBot.define do
  factory :user do
    nickname               {"taro"}
    email                  {"[email protected]"}
    password               {"00000000"}
    password_confirmation  {"00000000"}
  end
end

⑤ Run the test

Terminal


bundle exec rspec spec/models/user_spec.rb

Recommended Posts

Let's unit test with [rails] Rspec!
[Rails] Test with RSpec
[Rails5] Rspec -Unit test when nesting-
[Ruby on Rails] View test with RSpec
[Ruby on Rails] Controller test with RSpec
[Ruby on Rails] Model test with RSpec
Test Nokogiri with Rspec.
Unit test with Junit.
I rewrote the Rails tutorial test with RSpec
[RSpec] Let's use FactoryBot [Rails]
Test Active Strage with RSpec
[Rails] Test code using Rspec
Test GraphQL resolver with rspec
[Rails] About Rspec response test
Test with RSpec + Capybara + selenium + chromedriver
Copy and paste test with RSpec
[RSpec] Unit test (using gem: factory_bot)
Understand code coverage with Rspec, the Ruby on Rails test framework
[Rails] Unit test code for User model
Introduce RSpec and write unit test code
rails test fails with database reference error
Let's make an error screen with Rails
Rails tutorial test
Rspec Basics [Rails]
[Rails5] Rspec -validation-
About RSpec (Rails)
I want to test Action Cable with RSpec test
[For beginners] Test devise user registration with RSpec
Introducing Rspec with Ruby on Rails x Docker
Let's make a search function with Rails (ransack)
Rails beginners tried to get started with RSpec
[Rails] I want to test with RSpec. We support your step [Introduction procedure]
RSpec test code execution
[Rails] Test of star evaluation function using Raty [Rspec]
Introduction to RSpec 1. Test, RSpec
How to unit test with JVM with source using RxAndroid
Run Ruby on Rails RSpec tests with GitHub Actions
[My memo] Let's get along with Pry / DB with Rails
Rails deploy with Docker
Test run on rails
[Rails 6] RuntimeError with $ rails s
Testing model with RSpec
[Rails / RSpec] Write Model test (using Shoulda Matchers / FactoryBot)
Rails5 Rspec test error ArgumentError: Factory not registered: user
Handle devise with Rails
Java Unit Test Library-Artery-Sample
Run RSpec, Brakeman, Rubocop, Rails Best Practices with overcommit
Use webmock with Rspec
WebAPI unit test and integration test with SpringBoot + Junit5, 4 patterns
[Rails] Development with MySQL
Automatically test with Gauge
Load test with JMeter
Rails, RSpec installation procedure
Supports multilingualization with Rails!
Introduction to RSpec 4. Create test data with Factory Bot
Double polymorphic with Rails
I tried unit testing Rails app using RSpec and FactoryBot
About method matchers used in model unit test code (RSpec)
How to test a private method with RSpec for yourself
Let's cure the unit test like breathing (Apex trigger edition)
Rails Tutorial Chapter 14 Creating Relationship Test Data with Factory Bot