Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a single RSpec test?

I have the following file:

/spec/controllers/groups_controller_spec.rb 

What command in terminal do I use to run just that spec and in what directory do I run the command?

My gem file:

# Test ENVIRONMENT GEMS 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 

Spec file:

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 
like image 581
AnApprentice Avatar asked May 24 '11 20:05

AnApprentice


People also ask

How do I run a specific RSpec?

Running tests by their file or directory names is the most familiar way to run tests with RSpec. RSpec can take a file name or directory name and run the file or the contents of the directory. So you can do: rspec spec/jobs to run the tests found in the jobs directory.

How do I run a RSpec test in terminal?

Open your terminal, cd into the project directory, and run rspec spec . The spec is the folder in which rspec will find the tests. You should see output saying something about “uninitialized constant Object::Book”; this just means there's no Book class.

How do I run RSpec on Windows?

Installing RSpecBoot up your terminal and punch in gem install rspec to install RSpec. Once that's done, you can verify your version of RSpec with rspec --version , which will output the current version of each of the packaged gems. Take a minute also to hit rspec --help and look through the various options available.


2 Answers

Usually I do:

rspec ./spec/controllers/groups_controller_spec.rb:42 

Where 42 represents the line of the test I want to run.

EDIT1:

You could also use tags. See here.

EDIT 2:

Try:

bundle exec rspec ./spec/controllers/groups_controller_spec.rb:42 
like image 162
apneadiving Avatar answered Oct 11 '22 23:10

apneadiving


With Rake:

rake spec SPEC=path/to/spec.rb 

(Credit goes to this answer. Go vote him up.)

EDIT (thanks to @cirosantilli): To run one specific scenario within the spec, you have to supply a regex pattern match that matches the description.

rake spec SPEC=path/to/spec.rb \           SPEC_OPTS="-e \"should be successful and return 3 items\"" 
like image 20
Grant Birchmeier Avatar answered Oct 12 '22 01:10

Grant Birchmeier