我有以下文件:

/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/controllers/groups_controller_spec.rb:<line_number>

行号应该是“描述”或“it”行的行号,这样它将运行该特定块中的测试。 相反,它将执行line_number旁边的所有行。

此外,您可以创建自定义名称的块,然后只能执行这些块。

其他回答

通常我会这样做:

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

其中42表示我要运行的测试行。

你也可以使用标签。在这里看到的。

使用bundle exec:

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

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\""

在rails 5中,

我使用这种方式运行单个测试文件(所有测试在一个文件中)

rails test -n /TopicsControllerTest/ -v

类名可以用来匹配所需的文件TopicsControllerTest

我的类类TopicsControllerTest < ActionDispatch::IntegrationTest

输出:

如果你愿意,你可以调整正则表达式来匹配单个测试方法\TopicsControllerTest#test_Should_delete\

rails test -n /TopicsControllerTest#test_Should_delete/ -v

从rspec 2开始,你可以使用以下代码:

# in spec/spec_helper.rb
RSpec.configure do |config|
  config.filter_run :focus => true
  config.run_all_when_everything_filtered = true
end

# in spec/any_spec.rb
describe "something" do
  it "does something", :focus => true do
    # ....
  end
end

从项目的根目录运行命令:

# 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

此外,您可以使用——example (-e)选项来运行特定的测试,这些测试部分或完全匹配给定测试路径的'it'、'describe'或'context'块中的文本标签:

# 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

这将运行块内嵌套的所有测试,标签与示例选项接收到的字符串参数匹配。

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.

附加阅读(文档链接)

例子的选择 格式选项