RSpec Interactive
A Pry console capable of running specs.
Installation & Configuration
Install:
gem 'rspec-interactive'
Add a config file which configures RSpec and RSpec::Interactive, for example spec/rspec_interactive.rb
:
RSpec::Interactive.configure do |config|
# Directories to watch for file changes. When a file changes, it will be reloaded like `load 'path/to/file'`.
config.watch_dirs += ["app", "lib", "config"]
# This block is invoked on startup. RSpec configuration must happen here so that it can be cached and reloaded before each test run.
config.configure_rspec do
require './spec/spec_helper.rb'
end
# Invoked whenever a class is loaded due to a file change in one of the watch_dirs.
config.on_class_load do |clazz|
clazz.clear_validators! if clazz < ApplicationRecord
end
# Invoked before each invocation of RSpec. Can also be manually invoked by typing `refresh` in the console.
# Any modified/added files will be loaded via `load` before invoking.
config.refresh do
FactoryBot.reload
Rails.application.reloader.reload!
end
end
Update .gitignore
echo '.rspec_interactive_history' >> .gitignore
A Note About FactoryBot
It is not possible to reload a file containing FactoryBot factory definitions via load
because FactoryBot does not allow factories to be redefined. Be carefule not to add any directories to watch_dirs
which contain factory definitions. Instead, you should configure the location of your factories like the following in your spec_helper.rb
:
FactoryBot.definition_file_paths = %w(spec/factories)
FactoryBot.find_definitions # Only if not using Rails
Then add the following to your RSpec Interactive config
RSpec::Interactive.configure do |config|
config.refresh do
FactoryBot.reload
end
end
This will cause factories to be reloaded before each test run and also whenever the refresh
command is invoked in the console.
See: https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md#configure-your-test-suite
See: https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md#rails-preloaders-and-rspec
Usage
Optionally, specify a config file with --config <config-file>
.
bundle exec rspec-interactive [--config <config-file>]
Example Usage In This Repo
Start:
bundle exec rspec-interactive
Run a passing spec:
[1] pry(main)> rspec examples/passing_spec.rb
Run a failing spec:
[3] pry(main)> rspec examples/failing_spec.rb
Run an example group:
[5] pry(main)> rspec examples/passing_spec.rb:4
Run multiple specs:
[6] pry(main)> rspec examples/passing_spec.rb examples/failing_spec.rb
Debug a spec (use exit
to resume while debugging):
[7] pry(main)> rspec examples/debugged_spec.rb
Run multiple specs using globbing (use exit
to resume while debugging):
[8] pry(main)> rspec examples/*_spec.rb
Exit:
[9] pry(main)> exit
Running Tests
bundle exec bin/test
Releasing
./scripts/release.sh <version>