我有以下文件:

/spec/controllers/groups_controller_spec.rb

我在终端中使用什么命令来运行该规范,在什么目录中运行该命令?

我的宝石文件:

# 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

规范文件:

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

当前回答

你可以这样做:

 rspec/spec/features/controller/spec_file_name.rb
 rspec/spec/features/controller_name.rb         #run all the specs in this controller

其他回答

你可以这样做:

 rspec/spec/features/controller/spec_file_name.rb
 rspec/spec/features/controller_name.rb         #run all the specs in this controller

您可以将一个正则表达式传递给spec命令,该命令将只运行与您提供的名称匹配的块。

spec path/to/my_spec.rb -e "should be the correct answer"

2019更新:Rspec2从'spec'命令切换到'rspec'命令。

我偏爱的运行特定测试的方法略有不同—— 我添加了这些行

  RSpec.configure do |config|
    config.filter_run :focus => true
    config.run_all_when_everything_filtered = true
  end

到我的spec_helper文件。

现在,每当我想要运行一个特定的测试(或上下文或规范)时,我可以简单地向它添加“focus”标签,并正常运行我的测试——只有被聚焦的测试才会运行。如果我删除所有的焦点标签,run_all_when_everything_filtered就会生效,并正常运行所有测试。

它不像命令行选项那样快速和简单——它需要您为想要运行的测试编辑文件。但我觉得这能给你更多的控制。

有很多选择:

rspec spec                           # All specs
rspec spec/models                    # All specs in the models directory
rspec spec/models/a_model_spec.rb    # All specs in the some_model model spec
rspec spec/models/a_model_spec.rb:nn # Run the spec that includes line 'nn'
rspec -e"text from a test"           # Runs specs that match the text
rspec spec --tag focus               # Runs specs that have :focus => true
rspec spec --tag focus:special       # Run specs that have :focus => special
rspec spec --tag focus ~skip         # Run tests except those with :focus => true

Rake:

rake spec SPEC=path/to/spec.rb

(这个答案值得称赞。去给他投票吧。)

编辑(感谢@cirosantilli):要在规范中运行特定场景,必须提供与描述匹配的正则表达式模式匹配。

rake spec SPEC=path/to/spec.rb \
          SPEC_OPTS="-e \"should be successful and return 3 items\""