我有以下文件:

/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

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

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

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


通常我会这样做:

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

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

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

使用bundle exec:

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

假设你在一个rspec 2的rails 3项目中,从rails根目录:

  bundle exec rspec spec/controllers/groups_controller_spec.rb 

应该没问题。我厌倦了输入,所以我创建了一个别名来缩短'bundle exec rspec'到'bersp'

'bundle exec'可以加载gem文件中指定的gem环境:http://gembundler.com/

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就会生效,并正常运行所有测试。

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


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

我使用这个守护宝石自动运行我的测试。它在对测试文件进行创建或更新操作后执行测试。

https://github.com/guard/guard-test

或 通常可以使用以下命令运行

rspec spec / controllers / groups_controller_spec rb。


有很多选择:

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

你可以这样做:

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

@ apne潜水的答案是一个整洁的解决这个问题的方法。然而,现在我们在Rspec 3.3中有了一个新方法。我们可以简单地运行rspec spec/unit/baseball_spec。Rb [#context:#it]而不是行号。从这里开始:

RSpec 3.3引入了一种识别示例的新方法[…] 例如,以下命令: $ rspec spec/unit/baseball_spec.rb[1:2,1:4] …将运行在spec/unit/baseball_spec.rb中定义的第一个顶级组下定义的第二个和第四个示例或组。

所以与其这样做 rspec规范/单位/ baseball_spec。第42行测试是第一个测试,我们可以简单地做 rspec规范/单位/ baseball_spec。Rb[1:1]或rspec spec/unit/baseball_spec。Rb[1:1:1]取决于测试用例的嵌套程度。


从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

在rails 5中,

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

rails test -n /TopicsControllerTest/ -v

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

我的类类TopicsControllerTest < ActionDispatch::IntegrationTest

输出:

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

rails test -n /TopicsControllerTest#test_Should_delete/ -v

对于模型,它将只在第5行上运行case

bundle exec rspec spec/models/user_spec.rb:5

对于控制器:它将只在第5行上运行case

bundle exec rspec spec/controllers/users_controller_spec.rb:5

对于信号模型或控制器,从上面去掉行号

在所有模型上运行case

bundle exec rspec spec/models

在所有控制器上运行case

bundle exec rspec spec/controllers

运行所有案例

 bundle exec rspec 

不知道这个已经可用了多长时间,但是有一个Rspec配置用于运行过滤-所以现在你可以把它添加到你的spec_helper.rb:

RSpec.configure do |config|
  config.filter_run_when_matching :focus
end

然后在it、context或describe中添加一个focus标签,只运行该块:

it 'runs a test', :focus do
  ...test code
end

RSpec文档:

https://www.rubydoc.info/github/rspec/rspec-core/RSpec/Core/Configuration#filter_run_when_matching-instance_method


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

# 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.

附加阅读(文档链接)

例子的选择 格式选项


你可以使用

 rspec spec/controllers/groups_controller_spec.rb:<line_number>

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

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


对于单个示例的规格文件,您需要在最后添加行号,例如

rspec spec/controllers/api/v1/card_list_controller_spec.rb:35

对于单个文件,您可以指定您的文件路径,例如

rspec spec/controllers/api/v1/card_list_controller_spec.rb

对于spec文件夹中的整个Rspec示例,您可以尝试使用此命令

bundle exec rspec spec