我有以下文件:
/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 --help
会给你答案:
通常我会这样做:
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/to/spec.rb
(归功于 this answer。去投票给他。)
编辑(感谢@cirosantilli):要在规范中运行一个特定场景,您必须提供与描述匹配的正则表达式模式匹配。
rake spec SPEC=path/to/spec.rb \
SPEC_OPTS="-e \"should be successful and return 3 items\""
SPEC=path/to/spec.rb:42
在给定的行号上运行测试,但似乎任何 it_behaves_like
测试也会运行(错误?)。
您可以将正则表达式传递给 spec 命令,该命令只会运行与您提供的名称匹配的 it
块。
spec path/to/my_spec.rb -e "should be the correct answer"
2019 年更新:Rspec2 从“spec”命令切换到“rspec”命令。
it
、context
或 describe
声明之后没有遗漏 do
。
有很多选择:
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.configure do |config|
config.filter_run :focus => true
config.run_all_when_everything_filtered = true
end
到我的 spec_helper 文件。
现在,每当我想运行一个特定的测试(或上下文或规范)时,我可以简单地将标签“焦点”添加到它,并正常运行我的测试 - 只有焦点测试会运行。如果我删除所有焦点标记,run_all_when_everything_filtered
就会启动并正常运行所有测试。
它不像命令行选项那样快速和简单 - 它确实需要您编辑要运行的测试的文件。但我觉得它给了你更多的控制权。
不确定它可用多长时间,但有一个用于运行过滤的 Rspec 配置 - 所以现在您可以将它添加到 spec_helper.rb
:
RSpec.configure do |config|
config.filter_run_when_matching :focus
end
然后将焦点标记添加到 it
、context
或 describe
以仅运行该块:
it 'runs a test', :focus do
...test code
end
RSpec 文档:
rspec
命令运行带有适当参数的规范以指示哪个
@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]
。
zsh: no matches found
错误。例如rspec "spec/unit/baseball_spec.rb[1:1]"
对于规范文件的单个示例,您需要在最后添加行号,例如
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
从项目的根目录运行命令:
# 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
块)以绿色或红色表示它是通过还是失败。这可以更好地确认您的示例参数与您打算运行的规范相匹配,并且在示例参数匹配许多块标签和/或匹配块包含许多嵌套示例。
附加阅读(文档链接)
示例选项
格式选项
对于模型,它将仅在第 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
在轨道 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
您可以使用
rspec spec/controllers/groups_controller_spec.rb:<line_number>
行号应该是“描述”或“它”行的行号,以便它将运行该特定块中存在的测试。相反,它将执行 line_number 旁边的所有行。
您也可以使用自定义名称创建块,然后只能执行这些块。
从 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
鉴于您在使用 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”命令。
我使用这个保护 gem 来自动运行我的测试。它在对测试文件进行创建或更新操作后执行测试。
https://github.com/guard/guard-test
或者通常您可以使用以下命令运行
rspec 规范/控制器/groups_controller_spec.rb
你可以这样做:
rspec/spec/features/controller/spec_file_name.rb
rspec/spec/features/controller_name.rb #run all the specs in this controller
不定期副业成功案例分享
rspec
失败,因为您系统上的版本比您的 gemfile 中的版本更新。