[RUBY] RSpec Setup

RSpec setup

References Everyday Rails-Introduction to Rails Testing with RSpec https://leanpub.com/everydayrailsrspec-jp

Environment Mac Ruby 2.4.9 Rails 5.1.1

This is a memorandum for the introduction of Rspec.

Install RSpec

Open GemFile and make it available in test and development environments Add RSpec to the group below.

GemFile.


group :development, :test do
  #Please change the additional version specification according to each environment.
  gem 'rspec-rails', '~> 3.6.0'
end

Enter the following command

Terminal.


bin/rails g rspec:install

Then the following folders are created.

Terminal.


Running via Spring preloader in process 53239
      create  .rspec
      create  spec
      create  spec/spec_helper.rb
      create  spec/rails_helper.rb

Now you are ready.

Next, change the RSpec output that flows to the terminal during testing to an easy-to-read document format Describe the following in .rspec.

.rspec


#It is listed by default.
--require spec_helper
#Add this
--format documentation

Introducing rspec binstub

What is binstub? A handyman who can start the application quickly and can easily call bin / ~ from bundle exec ~.

Reference URL https://qiita.com/tanaka51/items/bc22c1c364202d3cce4e

Add to the development environment and bundle

GemFile.


group :development do
  gem 'spring-commands-rspec'
end

Then type the following command in the terminal, RSpec is newly added in the bin directory directly under the Rails application directory.

Terminal.


bundle exec spring binstub rspec

When you're done so far

Terminal.


bin/rspec

Check if the test runs with. It's super easy because it runs the tests in the rspec directory all at once. And the second execution is insanely fast.

Test plugin for VS Code people

Rails Run Specs

This is a convenient extension that opens the test code and VS Code will test the screen with cmd + shift + t!

Highly recommended

Have rails g create an RSpec file with you

Ask them to create a RSpec test file when they add code to their application with the generator. However, it is not good to create unused directories and test files, so at the same time, set not to create unused ones.

Add to config / application.rb.

config/application.rb


module Projects
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 5.1

    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.


    #Add below from here
    config.generators do |g|
      #Also create a RSpec test file when generating
      g.test_framework :rspec,
      #Skip creating files to create records in the test database
      fixtures: false,
      #Specify not to create view specs
      view_specs: false,
      #Specify not to create specs for helper files
      helper_specs: false,
      #Omitting the creation of spec files for ruding
      routing_specs: false
    end
  #So far
  end
end

If you want to add it manually Create the directory name and file name of the file you want to test together If you want to test app / helpers / projects_helper.rb, create spec / helpers / projects_helper_spec.rb.

Finally, delete the test directory that Rails provides by default!

By the way, if you want to create only RSpec file, enter the following generate command and it will be created. In the following cases, the model directory will be created in the RSpec directory and the user's RSpec file will be created.

spec/models/user_spec.rb

Terminal.


bin/rails g rspec:model user

#result
      create  spec/models/user_spec.rb

That's all for this time.

If you have any mistakes, errors, or advice, we would appreciate it if you could teach us. Thank you very much.

Recommended Posts

RSpec setup
RSpec Setup
Introduction to RSpec 2. RSpec setup
Keycloak setup
Introducing RSpec
RSpec preparation
Install RSpec
RSpec Basics
Rspec, TDD (1)
Hello RSpec
Rspec introduction memo_Rails
JavaCC setup procedure
CentOS8 basic setup
Rspec Basics [Rails]
[Rails5] Rspec -validation-
About RSpec (Rails)