ChatGPT解决这个技术问题 Extra ChatGPT

如何运行单个 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
你可以像 bundle exec rspec ./spec/controllers/groups_controller_spec.rb:6 一样运行你的测试,它只运行这个特定的测试。更多信息:kolosek.com/rails-rspec-setup
bundle exec rspec spec --help 会给你答案:

R
Rob Bednark

通常我会这样做:

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

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

您也可以使用标签。请参阅here

使用 bundle exec

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

谢谢尝试,当我执行 rake spec /spec/path...:XX 我收到错误 /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby -S bundle exec rspec ./spec/controllers/groups_controller_spec.rb ./spec/controllers/incoming_mails_controller_spec.rb ./spec/lib/mailing_job/mailingjob_find_reply_spec.rb ./spec/models/group_model_spec.rb ./spec/models/user_model_spec.rb
如果我尝试仅使用 RSPEC,我会收到此错误:“$ rspec spec/controllers/groups_controller_spec.rb:19 /Library/Ruby/Gems/1.8/gems/bundler-1.0.0/lib/bundler/runtime.rb:27 :in `setup': 你已经激活了 rspec-core 2.6.2,但是你的 Gemfile 需要 rspec-core 2.6.0。考虑使用 bundle exec。(Gem::LoadError)"
在这种情况下,您可以尝试“bundle exec rspec spec/controllers/groups_controller_spec.rb:19”
bundle exec 工作但为什么?这是一种避免这种情况的方法吗?
这不是 hack,它确保您使用与您在 gemfile 中声明的版本完全相同的版本。在您的情况下,仅 rspec 失败,因为您系统上的版本比您的 gemfile 中的版本更新。
C
Community

使用耙子:

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

这是更好的答案,因为它使用“rake spec”命令而不是“rspec”命令。这意味着每次都正确地重新初始化测试数据库(如果您使用 'rspec...' 则不会发生这种情况)
您可以使用 SPEC=path/to/spec.rb:42 在给定的行号上运行测试,但似乎任何 it_behaves_like 测试也会运行(错误?)。
M
Michael Durrant

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

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

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


谢谢,我试过了,但它出错了: $ rake spec spec/controllers/incoming_mails_controller_spec.rb -e “应该成功并返回 3 个项目” rake 中止! (eval):1:in `standard_rake_options': 编译错误 (eval):1: 语法错误,意外的 tIDENTIFIER,期望 $end 应该成功并返回 3 个项目
更新了实际的规范文件想法?
如果您有编译错误,则说明您的规范不是有效的 ruby。确保在 itcontextdescribe 声明之后没有遗漏 do
这是“规格”,而不是“耙规格”。
这应该是正确的答案,与行号有关是严重错误的 - 无论如何
M
Michael Durrant

有很多选择:

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

G
GlyphGryph

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

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

到我的 spec_helper 文件。

现在,每当我想运行一个特定的测试(或上下文或规范)时,我可以简单地将标签“焦点”添加到它,并正常运行我的测试 - 只有焦点测试会运行。如果我删除所有焦点标记,run_all_when_everything_filtered 就会启动并正常运行所有测试。

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


我绝对喜欢这种风格,因为我通常通过 Rubymine/intelliJ 运行测试。我也喜欢这种方法,因为它类似于在 jasmine 中使用 fit/xit / with gulp
e
eazy_beans

不确定它可用多长时间,但有一个用于运行过滤的 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


这需要改变基础设施。并记得把它改回来。我建议不要这样做,只使用 rspec 命令运行带有适当参数的规范以指示哪个
C
Community

@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 中定义的第一个顶级组。

因此,不是在它(第 42 行的测试)是第一个测试的地方执行 rspec spec/unit/baseball_spec.rb:42,我们可以根据测试用例的嵌套程度简单地执行 rspec spec/unit/baseball_spec.rb[1:1]rspec spec/unit/baseball_spec.rb[1:1:1]


请注意,如果您的 shell 是 ZSH(现在所有 Mac 的默认设置),您需要将最后一个参数括在引号中以防止出现 zsh: no matches found 错误。例如rspec "spec/unit/baseball_spec.rb[1:1]"
F
Foram

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

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

A
Allison

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

# 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

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

使用示例选项时,我建议您也将 --format documentation(简写:-f documentation)附加到您的捆绑命令(例如,bundle exec rspec spec/some_file.rb -e spaghetti -f documentation)。文档格式将正常的 ./F 输出替换为易于阅读的漂亮打印细分,显示您正在运行的示例的嵌套块标签,并输出每个示例的打印标签(it 块)以绿色或红色表示它是通过还是失败。这可以更好地确认您的示例参数与您打算运行的规范相匹配,并且在示例参数匹配许多块标签和/或匹配块包含许多嵌套示例。

附加阅读(文档链接)

示例选项

格式选项


S
Sandeep Kapil

对于模型,它将仅在第 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 

A
Alupotha

在轨道 5 中,

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

rails test -n /TopicsControllerTest/ -v

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

我的班级class TopicsControllerTest < ActionDispatch::IntegrationTest

输出 :

https://i.stack.imgur.com/oBHpr.png

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

rails test -n /TopicsControllerTest#test_Should_delete/ -v

A
Akshay Dhobale

您可以使用

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

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

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


R
Rudi

从 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

M
MissingHandle

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


R
Rameshwar Vyevhare

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

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

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

rspec 规范/控制器/groups_controller_spec.rb


P
Prabhakar Undurthi

你可以这样做:

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