Introducing Rspec with Ruby on Rails x Docker

Target

--rails6 × I want to install rspec in docker environment

Premise

procedure

1. Add gem to Gemfile

Add the following gem to: develop,: test gem "rspec-rails" gem "factory_bot_rails"

Gemfile


group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  #Test framework
  gem "rspec-rails"
  gem "factory_bot_rails"
end

2. Install rspec from a different terminal than the one running the server

MacBook-Air app name% docker-compose run web rails g rspec:install
Starting app name_db_1 ... done
Running via Spring preloader in process 64
      create  .rspec
      create  spec
      create  spec/spec_helper.rb
      create  spec/rails_helper.rb

This will create a configuration file in your app's directory

Let's create a test at once

Try to generate it with a generator to write the spec of the model

First, the related files are automatically generated with the following command. model is its own model.

MacBook-Air app name% docker-compose run web rails g rspec:model model name
Starting app name_db_1 ... done
Running via Spring preloader in process 64
      create  spec/models/reception_spec.rb
      invoke  factory_bot
      create    spec/factories/receptions.rb

If you can generate it, write a test for validation this time

spec/factories/reception.rb


FactoryBot.define do
  factory :reception do
    name {"Sample visitor 1"}
    purpose {"interview"}
    organization {"Sample Co., Ltd."}
  end
end

spec/models/reception_spec.rb


RSpec.describe Reception, type: :model do
  reception = FactoryBot.create(:reception)

  it 'reception instance is valid' do
    expect(reception).to be_valid
  end
end

After adding the spec,

MacBook-Air app name% docker-compose run web bundle exec rspec

Execution completed with

MacBook-Air app name% docker-compose run web bundle exec rspec
Starting app name_db_1 ... done
.

Finished in 0.22889 seconds (files took 6.32 seconds to load)
1 example, 0 failures

Write a request spec (API test)

Before writing the request spec, generate the necessary files with the generator.

docker-compose run web rails g rspec:request controller name/Model name
Starting heytaisho_db_1 ... done
Running via Spring preloader in process 66
      create  spec/requests/receptions_spec.rb

Open and edit the generated file

References

https://qiita.com/Ushinji/items/522ed01c9c14b680222c

Recommended Posts

Introducing Rspec with Ruby on Rails x Docker
[Ruby on Rails] Controller test with RSpec
[Ruby on Rails] Model test with RSpec
Introducing Rspec, a Ruby on Rails test framework
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
Run Ruby on Rails RSpec tests with GitHub Actions
Notes on using FCM with Ruby on Rails
Ruby on Rails development environment construction with Docker + VSCode (Remote Container)
Understand code coverage with Rspec, the Ruby on Rails test framework
Docker x Rails 6 (memo)
Rails deploy with Docker
Publish the app made with ruby on rails
Notes on building Rails6 / PostgreSQL with Docker Compose
Ruby on Rails Elementary
Ruby on Rails basics
Incorporate Docker into your Ruby on Rails portfolio!
[Rails] Test with RSpec
[Rails] Procedure for linking databases with Ruby On Rails
Ruby On Rails Association
Determine the current page with Ruby on Rails
Ruby on Rails ✕ Docker ✕ MySQL Introducing Docker and docker-compose to apps under development
[Ruby on Rails] Upload multiple images with refile
I made a portfolio with Ruby On Rails
A memorandum when building an environment with Ruby3.0 x Rails6.1 x Docker x CentOS Stream
Docker the development environment of Ruby on Rails project
[Ruby on Rails] Delete s3 images with Active Strage
Error encountered with notes when deploying docker on rails
Solve the N + 1 problem with Ruby on Rails: acts-as-taggable-on
Build Rails (API) x MySQL x Nuxt.js environment with Docker
Created RSS / Atom format sitemap with Ruby on Rails
Ruby on rails learning record -2020.10.03
Portfolio creation Ruby on Rails
Ruby on rails learning record -2020.10.04
[Ruby on Rails] Debug (binding.pry)
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby on Rails config configuration
Ruby on Rails basic learning ①
[Ruby on Rails] about has_secure_password
Ruby on rails learning record-2020.10.07 ②
Run Rails whenever with docker
Commentary on partial! --Ruby on Rails
[Docker] Rails 5.2 environment construction with docker
Ruby on rails learning record-2020.10.07 ①
Cancel Ruby on Rails migration
Ruby on rails learning record -2020.10.06
Ruby on Rails validation summary
Ruby on Rails Basic Memorandum
[Docker] Use whenever with Docker + Rails
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (5)
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (6)
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (3)
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (2)
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (1)
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (4)
I tried installing Ruby on Rails related plugin with vim-plug
[Ruby on Rails] Add a column with a foreign key constraint
[Ruby on Rails] Implement login function by add_token_to_users with API
[Apple login] Sign in with Apple implementation procedure (Ruby on Rails)
Ruby on Rails Overview (Beginner Summary)