When your Ruby on Rails project is getting bigger your test suite as well. You need to test more of your business logic and sometimes you will use other gems that can help you with that. Most of the time you may need something like database_cleaner, capybara for feature tests or rspec-sidekiq to test your workers.
Adding new gems needed for testing often requires changes in RSpec configuration. You add a new line of config here and there in the spec_helper.rb or rails_helper.rb file and suddenly you have huge and hard to understand config file for RSpec.
I will show you how I organize my RSpec configuration directory structure to easily add or modify the RSpec configuration in a clean way.
Prepare RSpec config directory structure
I keep all of my configuration code related to RSpec in directory spec/support/config. You can create it.
Next step is to ensure the RSpec will read files from the config directory. In order to do it please ensure you have below line in your spec/rails_helper.rb file.
By default, it is commented out so please uncomment it.
Separate config files for different testing gems
Here I will show you examples of popular gems and how to keep their configuration clean. A few examples are on the video and many more code examples are in this article.
Configuration for Database Cleaner
For database_cleaner gem you can just create config file spec/support/config/database_cleaner.rb:
Configuration for Capybara
Here is my configuration of Capybara placed at spec/support/config/capybara.rb.
I have a few custom things here like easy option to switch between different browsers executing my tests by just adding tag like :selenium_chrome to test:
Configuration for FactoryBot (known as FactoryGirl)
FactoryBot config file for Ruby on Rails can be isolated at spec/support/config/factory_bot.rb:
Configuration for JSON Spec
If you have API endpoints in your application you may like the json_spec gem that can help you test JSON responses. Here is my config at spec/support/config/json_spec.rb
Configuration for RSpec Retry
Sometimes big projects have painful tests that randomly fail. The last rescue when we cannot make them stable and always green is to retry them a few times before marking them as failed. This is one option how to deal with flaky tests and we can use for that rspec-retry gem.
Here you can learn more about how to deal with flaky tests:
Configuration for Shoulda Matchers
Shoulda Matchers provides RSpec and Minitest-compatible one-liners that test common Rails functionality. Here is my config spec/support/config/shoulda_matchers.rb:
Configuration for VCR and WebMock
You can record your requests in testing with VCR or mock request with WebMock. This is my config spec/support/config/vcr.rb:
Configuration for Knapsack Pro to split test suite across parallel CI nodes
You can keep your page objects in a separate directory. Page objects can be later reused in multiple tests.
Create directory spec/support/page_objects. Here is example page object for billing page:
Then you can use the page object in your feature spec.
Configuration for shared examples
You can organize your RSpec shared examples in directory spec/support/shared_examples that will be autoloaded.
As you can see there are a lot of different useful gems for testing in RSpec. If we would keep all their configuration just in spec_helper.rb we would quickly get a messy file. Separation of config files can help us keep it clean and easy to maintain. Let me know in comments how you keep your config files sane.