如何运行单个RSpec测试?

时间:2011-05-24 20:45:48

标签: ruby-on-rails ruby ruby-on-rails-3 rspec

我有以下文件:

/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

17 个答案:

答案 0 :(得分:445)

通常我会这样做:

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

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

EDIT1:

您也可以使用标签。见here

编辑2:

尝试:

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

答案 1 :(得分:63)

耙子:

rake spec SPEC=path/to/spec.rb

(信用证转到this answer。去投票给他。)

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

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

答案 2 :(得分:56)

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

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

答案 3 :(得分:23)

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

  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将启动并正常运行所有测试。

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

答案 4 :(得分:23)

有很多选择:

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

答案 5 :(得分:6)

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

  

RSpec 3.3引入了一种识别示例[...]

的新方法      

例如,这个命令:

     

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

所以不要这样做 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]取决于测试用例的嵌套方式。

答案 6 :(得分:3)

在轨道5中,

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

rails test -n /TopicsControllerTest/ -v

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

我的班级class TopicsControllerTest < ActionDispatch::IntegrationTest

输出:

enter image description here

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

rails test -n /TopicsControllerTest#test_Should_delete/ -v

答案 7 :(得分:3)

不确定蜜蜂有多长时间可用,但是有用于运行筛选的Rspec配置-现在,您可以将其添加到spec_helper.rb

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

然后将焦点标签添加到itcontextdescribe以仅运行该块:

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

答案 8 :(得分:3)

您可以使用

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

行号应该是'describe'或'it'行的行号,以便它将运行该特定块中存在的测试。 相反,它将执行line_number旁边的所有行。

您还可以使用自定义名称创建块,然后只能执行这些块。

答案 9 :(得分:2)

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

# 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

答案 10 :(得分:2)

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

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

答案 11 :(得分: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

答案 12 :(得分:1)

对于型号,它仅在第5行运行案例

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

对于控制器:它仅在第5行运行案例

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

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

在所有型号上运行案例

bundle exec rspec spec/models

在所有控制器上运行案例

bundle exec rspec spec/controllers

运行所有案例

 bundle exec rspec 

答案 13 :(得分:0)

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

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

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

rspec spec / controllers / groups_controller_spec.rb

答案 14 :(得分:0)

您可以这样做:

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

答案 15 :(得分:0)

鉴于你在使用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'命令。

答案 16 :(得分:0)

另一个常见的错误是仍然将旧版的Rails应用程序升级或升级到Rails 5+,并将require 'spec_helper'放在每个测试文件的顶部。这应该更改为require 'rails_helper'。如果在rake任务(rake spec)和运行单个规范(rspec path/to/spec.rb)之间看到不同的行为,这是常见的原因

最好的解决方案是

1)确保在每个规格文件的顶部使用require 'rails_helper'-而不是较旧的require 'spec_helper' 2)使用rake spec SPEC=path/to/spec.rb语法

我认为应该在2020年的这个时候将较旧的rspec path/to/spec.rb社区视为时尚(但是当然,除了其他考虑之外,您还可以使用它) strong>

相关问题