--rails6 × I want to install rspec in docker environment
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
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
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
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
https://qiita.com/Ushinji/items/522ed01c9c14b680222c
Recommended Posts