rspec如何运行单个测试?

时间:2021-12-04 01:24:09

I have found all kinds of links online but none that are current and show how to run a single 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 dir 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

12 个解决方案

#1


402  

Usually I do:

我通常做的事:

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

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

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

EDIT1:

EDIT1:

You could also use tags. See here.

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

EDIT 2:

编辑2:

Try:

试一试:

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

#2


58  

With Rake:

Rake:

rake spec SPEC=path/to/spec.rb

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

这个答案值得称赞。他投票。)

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

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

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

#3


47  

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

您可以将regex传递给spec命令,该命令只运行与您提供的名称匹配的regex块。

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

#4


22  

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_filtered kicks in and runs all the tests as normal.

现在,每当我想运行一个特定的测试(或上下文或规范)时,我都可以简单地向它添加标记“focus”,并正常地运行我的测试——只有重点测试才会运行。如果我删除所有的焦点标记,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.

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

#5


19  

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

#6


5  

@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 /unit/baseball_spec。rb[#context:#it]而不是使用行号。来自:

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

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

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规范/单位/ baseball_spec美元。rb[1:2,1:4]……将运行第2和第4个示例或组定义在spec/unit/baseball_spec.rb中定义的第一个*组。

So instead of doing rspec spec/unit/baseball_spec.rb:42 where 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 /unit/baseball_spec。rb:42在第一个测试中(第42行测试),我们可以做rspec spec spec spec/unit/baseball_spec。rb(1:1)或rspec规范/单位/ baseball_spec。rb[1:1:1]取决于测试用例的嵌套方式。

#7


2  

In rails 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

我的类TopicsControllerTest < ActionDispatch: IntegrationTest

Output :

输出:

rspec如何运行单个测试?

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

如果需要,可以调整regex以匹配单个测试方法\TopicsControllerTest#test_Should_delete\

rails test -n /TopicsControllerTest#test_Should_delete/ -v

#8


1  

starting with rspec 2 you can use the following:

从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

#9


1  

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

对于model,它只在第5行运行case

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 

#10


0  

Given you're on a rails 3 project with rspec 2, From the rails root directory:

如果您正在使用rspec 2进行rails 3项目,请从rails根目录:

  bundle exec rspec spec/controllers/groups_controller_spec.rb 

should definitely work. i got tired of typing that so i created an alias to shorten 'bundle exec rspec' to 'bersp'

一定要工作。我已经厌倦了输入,所以我创建了一个别名,将“bundle exec rspec”缩短为“bersp”

'bundle exec' is so that it loads the exact gem environment specified in your gem file: http://gembundler.com/

“bundle exec”用于加载您的gem文件:http://gembundler.com/中指定的gem环境

Rspec2 switched from the 'spec' command to the 'rspec' command.

Rspec2从“spec”命令切换到“rspec”命令。

#11


0  

I use this guard gem to auto-run my test. It execute test after create or update operations on test file.

我使用这个保护gem自动运行我的测试。它在创建或更新测试文件的操作之后执行测试。

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

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

or usually you can run using following command

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

rspec spec/controllers/groups_controller_spec.rb

rspec规范/控制器/ groups_controller_spec.rb

#12


0  

You can do something like this:

你可以这样做:

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

#1


402  

Usually I do:

我通常做的事:

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

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

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

EDIT1:

EDIT1:

You could also use tags. See here.

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

EDIT 2:

编辑2:

Try:

试一试:

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

#2


58  

With Rake:

Rake:

rake spec SPEC=path/to/spec.rb

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

这个答案值得称赞。他投票。)

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

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

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

#3


47  

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

您可以将regex传递给spec命令,该命令只运行与您提供的名称匹配的regex块。

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

#4


22  

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_filtered kicks in and runs all the tests as normal.

现在,每当我想运行一个特定的测试(或上下文或规范)时,我都可以简单地向它添加标记“focus”,并正常地运行我的测试——只有重点测试才会运行。如果我删除所有的焦点标记,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.

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

#5


19  

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

#6


5  

@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 /unit/baseball_spec。rb[#context:#it]而不是使用行号。来自:

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

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

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规范/单位/ baseball_spec美元。rb[1:2,1:4]……将运行第2和第4个示例或组定义在spec/unit/baseball_spec.rb中定义的第一个*组。

So instead of doing rspec spec/unit/baseball_spec.rb:42 where 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 /unit/baseball_spec。rb:42在第一个测试中(第42行测试),我们可以做rspec spec spec spec/unit/baseball_spec。rb(1:1)或rspec规范/单位/ baseball_spec。rb[1:1:1]取决于测试用例的嵌套方式。

#7


2  

In rails 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

我的类TopicsControllerTest < ActionDispatch: IntegrationTest

Output :

输出:

rspec如何运行单个测试?

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

如果需要,可以调整regex以匹配单个测试方法\TopicsControllerTest#test_Should_delete\

rails test -n /TopicsControllerTest#test_Should_delete/ -v

#8


1  

starting with rspec 2 you can use the following:

从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

#9


1  

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

对于model,它只在第5行运行case

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 

#10


0  

Given you're on a rails 3 project with rspec 2, From the rails root directory:

如果您正在使用rspec 2进行rails 3项目,请从rails根目录:

  bundle exec rspec spec/controllers/groups_controller_spec.rb 

should definitely work. i got tired of typing that so i created an alias to shorten 'bundle exec rspec' to 'bersp'

一定要工作。我已经厌倦了输入,所以我创建了一个别名,将“bundle exec rspec”缩短为“bersp”

'bundle exec' is so that it loads the exact gem environment specified in your gem file: http://gembundler.com/

“bundle exec”用于加载您的gem文件:http://gembundler.com/中指定的gem环境

Rspec2 switched from the 'spec' command to the 'rspec' command.

Rspec2从“spec”命令切换到“rspec”命令。

#11


0  

I use this guard gem to auto-run my test. It execute test after create or update operations on test file.

我使用这个保护gem自动运行我的测试。它在创建或更新测试文件的操作之后执行测试。

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

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

or usually you can run using following command

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

rspec spec/controllers/groups_controller_spec.rb

rspec规范/控制器/ groups_controller_spec.rb

#12


0  

You can do something like this:

你可以这样做:

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