Continuation of Last time.
First, let's get ready to use RSpec.
Add ** rspec-rails ** to Gemfile and install.
Gemfile
group :development, :test do
gem 'rspec-rails'
end
$ bundle install
Check the test DB. If the following is not set, add it.
config/database.yml
test:
<<: *default
database: projects_test #If you are using MySQL or PostgreSQL
database: db/test.sqlite3 #If you are using SQLite
Creating a DB.
$ rails db:create:all
$ rails generate rspec:install
For now, RSpec setup is complete. thank you for your hard work! This command will generate the following file directories:
File | function |
---|---|
.rspec | setting file |
spec | Directory to put spec files |
spec/spec_helper.rb | Helper file used in the test |
spec/rails_helper.rb | Helper file used in the test |
Next, let's set up RSpec.
.rspec
--require spec_helper #spec_See helper
--format documentation # Make the test results easy to read
--warnings # Display all warnings (not an error, but will tell you what has been deprecated)
--warnings is omitted this time to eliminate information noise when running the test.
First, install binstub to speed up the test startup time.
Gemfile
group :development do
gem 'spring-commands-rspec'
end
$ bundle install
Next, set which spec file is automatically generated when the rails generate command is executed.
config/application.rb
config.generators do |g|
g.test_framework :rspec,
fixtures: false,
view_specs: false,
helper_specs: false,
routing_specs: false
end
The spec file set to false will not be automatically generated, and only the model and controller spec files will be automatically generated. You can also manually generate the spec file without automatic generation later.
If RSpec is installed correctly, you can run the test with the following command.
$ bin/rspec
Recommended Posts