Rails beginners tried to get started with RSpec

at first

Now that I've managed to complete the Rails tutorial, I'd like to introduce RSpec. First, I will introduce RSpec itself and the frameworks that accompany it.

What is RSpec in the first place?

RSpec is a testing framework for behavior-driven development in Ruby (wikipedia excerpt)

Behavior-driven development seems to be a development method in which the behavior is created first and the code is written later. In other words, you create something like a specification that describes the operation in advance, and implement the function according to it. It may be more like "writing a specification" than "writing a test".

Capybara

capybara is a testing framework that has been included since Rails 5.1. By combining with RSpec, it will automatically perform the browser operation that people actually check manually.

FactoryBot

FactoryBot is a gem that supports the creation of test data. There is a similar thing called Fixture that is included in the Rails standard for creating test data. Fixture can be described in YAML format with a structure similar to a database, but it is not good at complicated data control. On the other hand, Factory Bot makes it easier to control the state and relationships of data by using ActiveRecord callbacks. I interpreted Fixture as cutting out a scene from the database and FactoryBot as describing the movement of the data. If you make a mistake, please point it out.

Preparing RSpec description

First, add a gem called rspec-rails to the test of the Gemfile.

Gemfile.


gem 'rspec-rails'
$ bunble

After the gem installation is complete, run the following command.

$ rails g rspec:install

This completes the installation of RSpec itself. It's easy.

Capybara settings

Next, we will configure the settings for Capybara to be used in RSpec.

spec/spec/helper.rb


require 'capybara/rspec'

RSpec.configure do |config|
  config.before(:each, type: :system) do
    config.include Capybara::DSL
    config.include FactoryBot::Syntax::Methods
    driven_by :selenium_chrome_headless
  end

This time, I will use Headless Chrome as the browser with system spec, so I set selenium_chrome_headless.

Introducing FactoryBot

To install FactoryBot, describe it in the Gemfile as follows.

Gemfile.


gem 'factory_bot_rails'
$ bundle

This completes the introduction of FactoryBot.

Next, I would like to touch on how to write RSpec, but it seems to be long, so I will do it next time.

The content is simple in the first post, but I hope you will read it again next time.

Recommended Posts

Rails beginners tried to get started with RSpec
I tried to get started with WebAssembly
[Note] How to get started with Rspec
I tried to get started with Spring Data JPA
How to get started with creating a Rails app
How to get started with slim
I tried to get started with Swagger using Spring Boot
How to get started with Eclipse Micro Profile
[Rails] Test with RSpec
[For Rails beginners] Summary of how to use RSpec (get an overview)
Get started with Gradle
I started MySQL 5.7 with docker-compose and tried to connect
Get started with "Introduction to Practical Rust Programming" (Day 3)
I tried to introduce Bootstrap 4 to the Rails 6 app [for beginners]
Tutorial to create a blog with Rails for beginners Part 1
[1st] RSpec beginners tried to write ModelSpec as a beginner
[Rails] I tried to create a mini app with FullCalendar
How to get started with Gatsby (TypeScript) x Netlify x Docker
[Rails] I tried to implement batch processing with Rake task
Tutorial to create a blog with Rails for beginners Part 2
Now is the time to get started with the Stream API
How to get started with JDBC using PostgresSQL on MacOS
Tutorial to create a blog with Rails for beginners Part 0
Let's get started with parallel programming
I tried to interact with Java
Connect to Rails server with iPhone
Introducing React to Rails with react-rails
Java, arrays to start with beginners
Memo to get with Struts2 + Ajax
Let's unit test with [rails] Rspec!
Easy code review to get started with Jenkins / SonarQube: Static analysis
I tried to implement the image preview function with Rails / jQuery
How to get boolean value with jQuery in rails simple form
[Rails] What to do if you can't get parameters with form_with
I tried to make a group function (bulletin board) with Rails
I tried to introduce CircleCI 2.0 to Rails app
[Ruby on Rails] View test with RSpec
[Rails] How to use rails console with docker
Deploy to heroku with Docker (Rails 6, MySQL)
[Ruby on Rails] Controller test with RSpec
Get started with "Introduction to Practical Rust Programming" (Day 4) Call Rust from Ruby
[Rails] How to get the user information currently logged in with devise
[Ruby on Rails] Model test with RSpec
I tried to implement ModanShogi with Kinx
How to build Rails 6 environment with Docker
Swift beginners tried to implement microwave logic!
I tried to get the distance from the address string to the nearest station with ruby
[Rails] I want to test with RSpec. We support your step [Introduction procedure]
I want to test Action Cable with RSpec test
I rewrote the Rails tutorial test with RSpec
Let's get started with Java-Create a development environment ①
I tried to verify AdoptOpenJDK 11 (11.0.2) with Docker image
[Rails] Add page nation to table [For beginners]
I tried to make Basic authentication with Java
[Rails] rails new to create a database with PostgreSQL
I tried to manage struts configuration with Coggle
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I tried to manage login information with JMX
Introducing Rspec with Ruby on Rails x Docker
I tried to organize the session in Rails
Get child model with rails created_at desc scope