About RSpec introduction & closely related gems

Let's organize the gems related to the introduction of RSpec, which is a test gem for Ruby on Rails.

What is RSpec

A gem used to write Ruby on Rails test code. The actual gem name is "rspec-rails".

Used to test features such as new user registrations and logins, posting and editing messages.

Ruby has another test gem called mini_test installed as standard, but it seems that RSpec is the mainstream at the development site.

Install RSpec

Write the following in the Gemfile and enter bundle install in the terminal.

Gemfile


#group :development, :Describe in a group called test.
group :development, :test do
  gem 'rspec-rails', '~> 4.0.0'
end

Terminal


bundle install

Set RSpec

In the terminal, type the following to generate the directory and files.

Terminal


rails g rspec:install
#After executing the above, the following directories and files will be generated
    create  .rspec
      create  spec
      create  spec/spec_helper.rb
      create  spec/rails_helper.rb

Write the following in the .rspec file. Write it to visualize the execution result of the test code on the terminal.

.rspec


--require spec_helper
--format documentation #Add this sentence

That's it for RSpec.

Gem closely related to RSpec

FactoryBot and Faker are some of the gems I often use with RSpec.

What is Factory Bot?

It is a gem that can group instances, and the values ​​specified for the instances of each class are set in advance in other files and used in each test code.

As with RSpec, write it in the group group: development,: test in the Gemfile before installing.

Gemfile


group :development, :test do
  gem 'factory_bot_rails'
end

Terminal


bundle install

As a brief introduction, FactoryBot is used by describing it as follows. * Assuming that the app has a Users table

spec/factories/users.rb


FactoryBot.define do
  factory :user do
    name {"sample"}
    email {"sample@mail"}
    password {"sample777"}
  end
end

By writing in this way, you can prepare the user information to be used in the test in advance.

What is Faker

A gem that generates random values ​​will generate random values ​​such as email addresses, personal names, and passwords. As with RSpec, write it in the group group: development,: test in the Gemfile before installing.

Gemfile


group :development, :test do
  gem 'faker '
end

Terminal


bundle install

Faker will generate a random value like this.

spec/factories/users.rb


FactoryBot.define do
  factory :user do
    name { Faker::Name.initials(number: 5) }
    email { Faker::Internet.free_email }
    password { Faker::Internet.password(min_length: 6) }
  end
end

What you can do with FactoryBot and Faker

You can instantiate a random value each time you run the test code. If the app to be tested has a user management function, I think that columns such as email are prepared in the table, but when performing multiple tests, such as "There are already duplicate instances of email" It helps prevent unintended test errors.

So far, I'd like to talk about unit testing with RSpec in the next article.

Reference material

RSpec's official GitHub Official GitHub of FactoryBot (factory_bot_rails) Faker's official GitHub

Recommended Posts

About RSpec introduction & closely related gems
Rspec introduction memo_Rails
About RSpec (Rails)
[Rails 6] About main gems
Introduction to RSpec 1. Test, RSpec
Introduction to RSpec 2. RSpec setup
Introduction to RSpec 6. System specifications
Introduction to RSpec 3. Model specs
[Rails] About Rspec response test
About Spring DI related annotations