Setup RSpec
Even though Rails uses Minitest per default, RSpec is the de-facto standard at Renuo. We love RSpec and we strongly suggest to use it.
Add the following gems to your Gemfile:
group :development, :test do
gem 'factory_bot_rails'
gem 'rspec-rails'
end
group :test do
gem 'shoulda-matchers'
gem 'simplecov', require: false
gem 'super_diff'
end
You should know exactly why you are adding each one of them and why is necessary.
- Also add
/coverage/
to your.gitignore
file. - Remove the
test
folder from your project (there will be one calledspec
later).
Configuration
-
Install rspec via
rails generate rspec:install
-
Create a bin stub with
bundle binstubs rspec-core
-
At the top of the
spec/spec_helper.rb
require 'simplecov' SimpleCov.start 'rails' do add_filter 'app/channels/application_cable/channel.rb' add_filter 'app/channels/application_cable/connection.rb' add_filter 'app/jobs/application_job.rb' add_filter 'app/mailers/application_mailer.rb' add_filter 'app/models/application_record.rb' add_filter '.semaphore-cache' enable_coverage :branch minimum_coverage line: 100, branch: 100 end
to run code coverage and exclude files with less then 5 lines of code.
-
Inside
spec/spec_helper.rb
we suggest you to uncomment/enable the following:config.disable_monkey_patching! config.default_formatter = 'doc' if config.files_to_run.one? config.profile_examples = 5 config.order = :random Kernel.srand config.seed config.define_derived_metadata do |meta| meta[:aggregate_failures] = true end
Please check the spec_helper template
-
Add the configurations:
# spec/rails_helper.rb: # after `require 'rspec/rails'` require 'capybara/rspec' require 'capybara/rails' require 'selenium/webdriver' require 'super_diff/rspec-rails' Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } # ... (omitted configs here) RSpec.configure do |config| # ... (omitted configs here) config.before do |example| ActionMailer::Base.deliveries.clear I18n.locale = I18n.default_locale Rails.logger.debug { "--- #{example.location} ---" } end config.after do |example| Rails.logger.debug { "--- #{example.location} FINISHED ---" } end config.before(:each, type: :system) do driven_by :rack_test end config.before(:each, type: :system, js: true) do driven_by ENV['SELENIUM_DRIVER']&.to_sym || :selenium_chrome_headless end end # config/application.example.yml test: # SELENIUM_DRIVER: 'selenium_chrome' SELENIUM_DRIVER: 'selenium_chrome_headless'
Please check the full rails_helper template to compare.
- Add the line
bundle exec rspec
tobin/check
Note: If you want to debug a spec, you can simply uncomment the line
SELENIUM_DRIVER
in the application.yml to not run it headless:
✅ Our first (green) test
We are now going to write a first test to ensure that the whole configuration is working:
bin/check
should be green ✅- Write the test
spec/system/health_spec.rb
- Run
bin/check
and the test should pass and coverage is 100%.
Commit and push your changes! 🎉
⭐️
/up
is the defualt Health check path for Rails. Read about it in the guides.
If you want to customize the health check and add more checks, you can easily override the classRails::HealthController
and add your own checks.
Here you find an example that checks also the database connection.
Verify
- Open the two apps
Check that you see a green page in each app.
Javascript error reporter
-
Create the module
spec/support/javascript_error_reporter.rb
-
Verify that
config.include JavaScriptErrorReporter, type: :system, js: true
is in yourrails_helper.rb
Please check the rails_helper template to compare.