Question
I have this RSpec file in my Rails project:
# spec/controllers/groups_controller_spec.rb
require 'spec_helper'
describe GroupsController do
include Devise::TestHelpers
describe "GET yourgroups" do
it "should be successful and return 3 items" do
Rails.logger.info 'HAIL MARRY'
get :yourgroups, :format => :json
response.should be_success
body = JSON.parse(response.body)
body.should have(3).items # @user1 has 3 permissions to 3 groups
end
end
end
My Gemfile includes rspec-rails:
group :development, :test do
gem "autotest"
gem "rspec-rails", "~> 2.4"
gem "cucumber-rails", ">=0.3.2"
gem "webrat", ">=0.7.2"
gem 'factory_girl_rails'
gem 'email_spec'
end
What terminal command should I use to run only this spec file, and from which directory should I run it?
Short Answer
By the end of this page, you will understand how to run a single RSpec test file, how to run one specific example using a line number, and where to execute the command in a Rails project. You will also learn common command variations and mistakes beginners often make.
Concept
RSpec is a testing framework for Ruby and Rails. Instead of running the entire test suite every time, you can run only the test file or example you are currently working on.
This matters because:
- it speeds up development
- it makes debugging easier
- it helps you focus on one failing test at a time
- it is the normal workflow in real Rails projects
In a Rails app, RSpec test files are usually stored in the spec/ directory. When you pass a specific file path to RSpec, it runs only that file.
For example:
bundle exec rspec spec/controllers/groups_controller_spec.rb
That tells Bundler to use the gems from your project and tells RSpec exactly which spec file to run.
If you want to run only one example inside that file, you can add a line number:
bundle exec rspec spec/controllers/groups_controller_spec.rb:7
RSpec will run the example closest to that line.
The command should normally be run from the root directory of your Rails project, which is the folder that contains files such as:
Gemfileapp/config/spec/
Running from the project root ensures that relative paths like spec/controllers/groups_controller_spec.rb work correctly.
Mental Model
Think of your test suite like a library.
- Running all specs is like reading every book in the building.
- Running one spec file is like going straight to one shelf.
- Running one example by line number is like opening one exact page.
RSpec lets you point directly to the part you care about right now, instead of searching through everything.
Syntax and Examples
The most common syntax is:
bundle exec rspec path/to/spec_file.rb
For your file:
bundle exec rspec spec/controllers/groups_controller_spec.rb
Run this from the Rails project root.
Run a single example by line number
bundle exec rspec spec/controllers/groups_controller_spec.rb:7
This runs the example nearest line 7.
Older command style
In older RSpec/Rails setups, you may also see:
bundle exec spec spec/controllers/groups_controller_spec.rb
Because your project uses rspec-rails ~> 2.4, this older style may exist in some tutorials or codebases. However, bundle exec rspec ... is the clearer and more commonly recognized form.
Example workflow
cd my_rails_app
bundle exec rspec spec/controllers/groups_controller_spec.rb
Explanation:
cd my_rails_appmoves into the Rails app root
Step by Step Execution
Consider this command:
bundle exec rspec spec/controllers/groups_controller_spec.rb
Here is what happens step by step:
bundle execloads the gem versions from your project'sGemfile.lock.rspecstarts the RSpec test runner.spec/controllers/groups_controller_spec.rbtells RSpec to load only that file.- RSpec reads the file:
require 'spec_helper'
describe GroupsController do
describe "GET yourgroups" do
it "should be successful and return 3 items" do
get :yourgroups, :format => :json
response.should be_success
end
end
end
- RSpec finds one example:
it "should be successful and return 3 items" do
- It runs that example.
- If it passes, you see output showing 1 example, 0 failures.
Real World Use Cases
Developers run single RSpec tests all the time in situations like these:
-
Debugging one failing controller spec
- You change one action and want to retest only its spec file.
-
Working on a new feature
- You write one spec first, then run just that spec repeatedly while implementing the code.
-
Fixing flaky tests
- You isolate one spec and run it several times.
-
Improving slow feedback loops
- Instead of waiting for hundreds of tests, you run the relevant file only.
-
CI failure reproduction
- A CI job reports one failing spec file, and you run that file locally to investigate.
-
Refactoring legacy code
- You focus on one spec area, such as
controllers,models, orservices, before running the whole suite later.
- You focus on one spec area, such as
Real Codebase Usage
In real projects, developers commonly combine single-spec execution with a few practical patterns.
Focused debugging
Run one file while changing code:
bundle exec rspec spec/controllers/groups_controller_spec.rb
Run one exact example
When a file contains many examples, use a line number:
bundle exec rspec spec/controllers/groups_controller_spec.rb:8
Guard clauses in test workflow
Developers often start small:
- run one example
- run one file
- run a related folder
- run the full suite
Validation after local changes
If you edit a controller action, you might run:
- the related controller spec file first
- then related request or model specs
- finally the whole suite before committing
Common team habits
In mature Rails codebases, you will often see scripts or aliases such as:
bin/rspec spec/controllers/groups_controller_spec.rb
This is common in newer apps. In older apps, bundle exec rspec is more typical.
Error-focused iteration
When a test fails, developers use the file path from the error output and rerun only that file or line. This shortens the fix-and-verify cycle significantly.
Common Mistakes
1. Running the command from the wrong directory
If you are not in the Rails project root, the relative path may not work.
Broken example:
cd spec/controllers
bundle exec rspec spec/controllers/groups_controller_spec.rb
Why it fails:
- the path is now wrong relative to your current directory
Better:
cd /path/to/your/app
bundle exec rspec spec/controllers/groups_controller_spec.rb
2. Forgetting bundle exec
Broken example:
rspec spec/controllers/groups_controller_spec.rb
This may use a global RSpec version instead of your app's version.
Better:
bundle exec rspec spec/controllers/groups_controller_spec.rb
3. Using the wrong file path
Broken example:
bundle exec rspec controllers/groups_controller_spec.rb
Better:
Comparisons
| Task | Command | When to use it |
|---|---|---|
| Run all specs | bundle exec rspec | Final verification or broader checks |
| Run one spec file | bundle exec rspec spec/controllers/groups_controller_spec.rb | While working on one area |
| Run one example by line | bundle exec rspec spec/controllers/groups_controller_spec.rb:7 | Precise debugging |
| Run a folder of specs | bundle exec rspec spec/controllers | Testing one category |
rspec vs spec
| Command | Typical usage |
|---|
Cheat Sheet
# Run all specs
bundle exec rspec
# Run one spec file
bundle exec rspec spec/controllers/groups_controller_spec.rb
# Run one example by line number
bundle exec rspec spec/controllers/groups_controller_spec.rb:7
# Run all controller specs
bundle exec rspec spec/controllers
Where to run the command
Run it from the Rails project root, the directory containing:
Gemfileapp/config/spec/
For your question
Use:
bundle exec rspec spec/controllers/groups_controller_spec.rb
Key rules
- Use
bundle execto ensure the correct gem versions - Use the path relative to the project root
- Add
:line_numberto run one example - In older setups,
bundle exec spec ...may also appear
FAQ
What command runs only one RSpec file?
Use:
bundle exec rspec spec/controllers/groups_controller_spec.rb
Where should I run the RSpec command?
Run it from the root of the Rails project, where the Gemfile and spec directory are located.
How do I run only one test inside a spec file?
Add the line number of the example:
bundle exec rspec spec/controllers/groups_controller_spec.rb:7
Why should I use bundle exec?
It ensures Ruby uses the gem versions installed for your project instead of globally installed gems.
Can I run a whole folder of specs?
Yes.
bundle exec rspec spec/controllers
My project is old. Should I use spec or rspec?
Older projects sometimes use bundle exec spec ..., but bundle exec rspec ... is the standard command to try first.
Does running one spec file skip Rails setup?
Mini Project
Description
Create a small Rails-style practice workflow for running targeted tests. The goal is to simulate how developers work on one failing spec at a time instead of running the full suite repeatedly. This helps you build confidence with RSpec commands and project structure.
Goal
Practice running one spec file and one specific example from the Rails project root.
Requirements
- Create or identify a Rails project directory that contains a
Gemfileandspec/folder. - Add a sample spec file inside
spec/controllers/. - Run only that spec file using an RSpec command.
- Run a single example from that file using a line number.
- Verify that both commands execute successfully from the project root directory.
Keep learning
Related questions
Calling a Class Method from an Instance in Ruby
Learn how to call a class method from an instance in Ruby using self.class, with examples, pitfalls, and practical usage patterns.
Calling an Overridden Monkey-Patched Method in Ruby
Learn how to call the original method when monkey patching in Ruby, including alias_method patterns, examples, pitfalls, and practical usage.
Convert a Unix Timestamp to Ruby DateTime
Learn how to convert Unix timestamps to Ruby DateTime and Time objects, with examples, differences, pitfalls, and practical Ruby usage.