Ruby-on-rails 如何运行单个 RSpec 测试?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6116668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:13:51  来源:igfitidea点击:

How to run a single RSpec test?

ruby-on-railsrubyruby-on-rails-3rspec

提问by AnApprentice

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

采纳答案by emc

Not sure how long this has bee available but there is an Rspec configuration for run filtering - so now you can add this to your spec_helper.rb:

不确定这有多久了,但有一个用于运行过滤的 Rspec 配置 - 所以现在你可以将它添加到你的spec_helper.rb

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

And then add a focus tag to the it, contextor describeto run only that block:

然后将焦点标记添加到it,contextdescribe仅运行该块:

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

RSpec documentation:

RSpec 文档:

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

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

回答by apneadiving

Usually I do:

通常我这样做:

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

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

where42代表我要运行的测试行。

EDIT1:

编辑1:

You could also use tags. See here.

你也可以使用标签。见这里

EDIT 2:

编辑2:

Try:

尝试:

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

回答by Grant Birchmeier

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.

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

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

回答by Douglas F Shearer

You can pass a regex to the spec command which will only run itblocks matching the name you supply.

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

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

2019 Update: Rspec2 switched from the 'spec' command to the 'rspec' command.

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

回答by Michael Durrant

There are many options:

有很多选择:

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

回答by GlyphGryph

My preferred method for running specific tests is slightly different - I added the lines

我运行特定测试的首选方法略有不同 - 我添加了这些行

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

To my spec_helper file.

到我的 spec_helper 文件。

Now, whenever I want to run one specific test (or context, or spec), I can simply add the tag "focus" to it, and run my test as normal - only the focused test(s) will run. If I remove all the focus tags, the run_all_when_everything_filteredkicks in and runs all the tests as normal.

现在,每当我想运行一个特定的测试(或上下文或规范)时,我只需将标签“焦点”添加到它,然后像往常一样运行我的测试 - 只有聚焦的测试才会运行。如果我删除所有焦点标签,run_all_when_everything_filtered则会正常启动并运行所有测试。

It's not quite as quick and easy as the command line options - it does require you to edit the file for the test you want to run. But it gives you a lot more control, I feel.

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

回答by Ingo

@apneadiving answer is a neat way of solving this. However, now we have a new method in Rspec 3.3. We can simply run rspec spec/unit/baseball_spec.rb[#context:#it]instead of using a line number. Taken from here:

@apneadiving 答案是解决此问题的巧妙方法。但是,现在我们在 Rspec 3.3 中有一个新方法。我们可以简单地运行rspec spec/unit/baseball_spec.rb[#context:#it]而不是使用行号。取自这里:

RSpec 3.3 introduces a new way to identify examples[...]

For example, this command:

$ rspec spec/unit/baseball_spec.rb[1:2,1:4]…would run the 2nd and 4th example or group defined under the 1st top-level group defined in spec/unit/baseball_spec.rb.

RSpec 3.3 引入了一种识别示例的新方法[...]

例如,这个命令:

$ rspec spec/unit/baseball_spec.rb[1:2,1:4]...将运行在 spec/unit/baseball_spec.rb 中定义的第一个顶级组下定义的第二个和第四个示例或组。

So instead of doing rspec spec/unit/baseball_spec.rb:42where it (test in line 42) is the first test, we can simply do rspec spec/unit/baseball_spec.rb[1:1]or rspec spec/unit/baseball_spec.rb[1:1:1]depending on how nested the test case is.

因此 rspec spec/unit/baseball_spec.rb:42,与其在它(第 42 行中的测试)是第一个测试的地方做,我们可以简单地做 rspec spec/unit/baseball_spec.rb[1:1]rspec spec/unit/baseball_spec.rb[1:1:1]取决于测试用例的嵌套方式。

回答by Alupotha

In rails 5,

在导轨 5 中,

I used this way to run single test file(all the tests in one file)

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

rails test -n /TopicsControllerTest/ -v

Class name can be used to match to the desired file TopicsControllerTest

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

My class class TopicsControllerTest < ActionDispatch::IntegrationTest

我的课 class TopicsControllerTest < ActionDispatch::IntegrationTest

Output :

输出 :

enter image description here

在此处输入图片说明

If You want you can tweak the regex to match to single test method \TopicsControllerTest#test_Should_delete\

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

rails test -n /TopicsControllerTest#test_Should_delete/ -v

回答by Sandeep Kapil

For model, it will run case on line number 5 only

对于模型,它只会在第 5 行上运行案例

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

For controller : it will run case on line number 5 only

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

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

For signal model or controller remove line number from above

对于信号模型或控制器,从上面删除行号

To run case on all models

在所有模型上运行案例

bundle exec rspec spec/models

To run case on all controller

在所有控制器上运行案例

bundle exec rspec spec/controllers

To run all cases

运行所有案例

 bundle exec rspec 

回答by Allison

Run the commands from your project's root directory:

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

# 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