问题描述
|
我有以下文件:
/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
解决方法
不确定蜜蜂有多长时间可用,但是有一个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文档:
https://www.rubydoc.info/github/rspec/rspec-core/RSpec/Core/Configuration#filter_run_when_matching-instance_method
,通常我会:
rspec ./spec/controllers/groups_controller_spec.rb:42
ѭ10代表我要运行的测试行。
编辑1:
您也可以使用标签。看这里。
编辑2:
尝试:
bundle exec rspec ./spec/controllers/groups_controller_spec.rb:42
,与耙:
rake spec SPEC=path/to/spec.rb
(信贷去这个答案。去投票给他。)
编辑(由于@cirosantilli):要在规范中运行一个特定方案,您必须提供与描述匹配的正则表达式模式匹配。
rake spec SPEC=path/to/spec.rb \\
SPEC_OPTS=\"-e \\\"should be successful and return 3 items\\\"\"
,您可以将正则表达式传递给spec命令,该命令将仅运行与您提供的名称匹配的5个块。
spec path/to/my_spec.rb -e \"should be the correct answer\"
2019更新:Rspec2从\'spec \'命令切换为\'rspec \'命令。
,有很多选择:
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文件。
现在,每当我想运行一个特定的测试(或上下文或规范)时,我都可以简单地在其上添加标签“ focus”,然后像平常一样运行我的测试-仅运行聚焦测试。如果删除所有焦点标签,则run_all_when_everything_filtered
插入并正常运行所有测试。
它不如命令行选项那么快速和容易-它确实需要您编辑要运行的测试的文件。我认为,但这给了您更多的控制权。
,@apneadiving答案是解决此问题的一种好方法。但是,现在Rspec 3.3中有了新方法。我们可以简单地运行rspec spec/unit/baseball_spec.rb[#context:#it]
而不使用行号。从这里拍摄:
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]
取决于测试用例的嵌套方式。
,在导轨5中
我使用这种方式来运行单个测试文件(所有测试都在一个文件中)
rails test -n /TopicsControllerTest/ -v
类名可用于匹配所需的文件TopicsControllerTest
我的课程class TopicsControllerTest < ActionDispatch::IntegrationTest
输出:
如果需要,可以调整正则表达式以匹配单个测试方法method27ѭ
rails test -n /TopicsControllerTest#test_Should_delete/ -v
,对于模型,它将仅在第5行运行case
bundle exec rspec spec/models/user_spec.rb:5
对于控制器:仅在第5行运行case
bundle exec rspec spec/controllers/users_controller_spec.rb:5
对于信号模型或控制器,从上方移开行号
在所有型号上运行案例
bundle exec rspec spec/models
在所有控制器上运行案例
bundle exec rspec spec/controllers
运行所有案例
bundle exec rspec
,从项目的根目录运行命令:
# 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
,从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 \'命令。
,我使用此保护宝石自动运行测试。在创建或更新测试文件后执行测试。
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