Run the commands from your project's root directory:
# run all specs in the project's spec folder
bundle exec rspec
# run specs nested under a directory, like controllers
bundle exec rspec spec/controllers
# run a single test file
bundle exec rspec spec/controllers/groups_controller_spec.rb
# run a test or subset of tests within a file
# e.g., if the 'it', 'describe', or 'context' block you wish to test
# starts at line 45, run:
bundle exec rspec spec/controllers/groups_controller_spec.rb:45
Additionally, you can use the --example
(-e
) option to run specific tests that partially or fully match text labels in your 'it', 'describe', or 'context' blocks for the given test path:
# run groups controller specs in blocks with a label containing 'spaghetti flag is false'
bundle exec rspec spec/controllers/groups_controller_spec.rb -e 'spaghetti flag is false'
# Less granularly, you can run specs for blocks containing a substring of text
# that matches one or more block labels, like 'spaghetti' or 'paghett'
bundle exec rspec spec/controllers/groups_controller_spec.rb -e spaghetti
This will run all the tests nested inside the blocks with labels matching the string argument received by the example option.
When using the example option, I recommend also appending --format documentation
(shorthand: -f documentation
) to your bundle command (e.g., bundle exec rspec spec/some_file.rb -e spaghetti -f documentation
). Documentation-formatting replaces the normal .
/F
output with an easy-to-read pretty printed breakdown showing the nested block labels for the examples you're running and outputs the printed label for each example (it
block) in green or red to denote whether it passed or failed. This provides better confirmation that your example argument matches the specs you intended to run, and it gives live visibility to which examples are passing/failing during longer test runs where the example argument matches many block labels and/or matched blocks contain many nested examples.
Additional Reading (Documentation Links)