[Rails] RSpec Kihon no Ki [Introduction]

Introduction

RSpec is Rails' standard testing framework. More gems are needed compared to minitest. However, if you put all the gems at once, it becomes ambiguous which gem works where and what. Therefore, when learning RSpec, I decided to take a method of adding gems one by one according to the content of the test.

RSpec and FactoryBot

Gem installation and configuration

Introduce RSpec and FactoryBot for generating test data. FactoryBot plays a fixture-like role in minitest. If you put spring-commands-rspec here, you can run RSpec from binstub and use spring to shorten the time it takes to start the test.

Gemfile.rb


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

After bundle install, generate the data for RSpec with the following command.

$ bundle exec rails generate rspec:install

Add RSpec settings. Write the settings in config/application.rb. Here, it is set whether to generate a file for RSpec when the generate command is executed. Whether it is true or false is changed depending on the project.

config/application.rb


module SampleApp
  class Application < Rails::Application
    .
    .
    .
    config.generators do |g|
      g.test_framework :rspec,
        controller_specs: true,
        fixtures: true,
        helper_specs: true,
        model_specs: true,
        request_spec: true,
        routing_specs: false,
        view_specs: false
    end
  end
end

Next, set up FactoryBot. Calling the FactoryBot class can be simplified by writing the following:

spec/rails_helper.rb


RSpec.configure do |config|
  config.include FactoryBot::Syntax::Methods
end
#Call FactoryBot class
user = FactoryBot.create(:user)

#Abbreviated version
user = create(:user)

Another setting for test data. Uncomment the following line. Files under spec/support/ can be read. Files that describe helpers for testing are placed here.

spec/rails_helper.rb


# The following line is provided for convenience purposes. It has the downside
# of increasing the boot-up time by auto-requiring all files in the support
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }

Finally, install RSpec's binstub. Now you can run tests on $ bin/rspec spec/ in addition to $ bundle exec rspec spec/.

$ bundle exec spring binstub rspec

#Execution command
$ bin/rspec spec/

Generate spec file

All the files used by RSpec have the filename * _spec.rb. We will prepare various types according to the controller and model. For example, the spec file for users_controller is generated as follows.

$ rails g rspec:controller users
    create  spec/controllers/users_controller_spec.rb

Preparation of test data

Use FactoryBot to define test data (instances) for various classes. For example, in the case of User class, define it in /spec/factories/users.rb.

/spec/factories/users.rb


FactoryBot.define do

  #Define test data michael of User model
  factory :michael, class: User do # Class:User may not be needed
    name { 'michael' }
    email { '[email protected]' }
  end

  #Prepare a lot of general-purpose data
  factory :user do
    sequence(:name) { |n| "name-#{n}"}
    sequence(:email) { |n| "test-#{n}@example.com"}
  end
end

Call the defined test data, store it in an instance variable, and use it in the test.

/spec/models/users_spec.rb


RSpec.describe User, type: :model do
  before do
    #Creating a model only
    @michael = build(:michael)
    #Record generation to DB
    @michael = create(:michael)
    
    #Utilization of general-purpose data
    @user1 = build(:user)
    @user2 = build(:user)
    @user3 = build(:user)
  end
  .
  .
  .
end

Continue

Recommended Posts

[Rails] RSpec Kihon no Ki [Introduction]
Rails book review app RSpec introduction
Rspec introduction memo_Rails
[Rails 6] cocoon_ introduction
Rspec Basics [Rails]
[Rails5] Rspec -validation-
About RSpec (Rails)
[Ruby on Rails] Until the introduction of RSpec
Introduction to RSpec 1. Test, RSpec
[Rails] Introduction of PAY.JP
[Rails] Test with RSpec
[Rails] devise introduction method
Rails, RSpec installation procedure
Introduction to RSpec 2. RSpec setup
[Rails] Introduction of devise Basics
Introduction to RSpec 5. Controller specs
Introduction to RSpec 6. System specifications
Introduction to RSpec 3. Model specs
[RSpec] Let's use FactoryBot [Rails]
[Rails] About Rspec response test
Introduction to RSpec-Everyday Rails Summary-