AnA*_*ice 290 ruby rspec ruby-on-rails ruby-on-rails-3
我有以下文件:
/spec/controllers/groups_controller_spec.rb
Run Code Online (Sandbox Code Playgroud)
在终端中使用什么命令来运行该规范以及在哪个目录中运行命令?
我的宝石文件:
# 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
Run Code Online (Sandbox Code Playgroud)
规格文件:
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
Run Code Online (Sandbox Code Playgroud)
apn*_*ing 464
通常我这样做:
rspec ./spec/controllers/groups_controller_spec.rb:42
Run Code Online (Sandbox Code Playgroud)
其中42代表我想要运行的测试行.
EDIT1:
您也可以使用标签.看到这里.
编辑2:
尝试:
bundle exec rspec ./spec/controllers/groups_controller_spec.rb:42
Run Code Online (Sandbox Code Playgroud)
Gra*_*ier 65
随着耙子:
rake spec SPEC=path/to/spec.rb
Run Code Online (Sandbox Code Playgroud)
(归功于这个答案.去投票吧.)
编辑(感谢@cirosantilli):要在规范中运行一个特定场景,您必须提供与描述匹配的正则表达式模式匹配.
rake spec SPEC=path/to/spec.rb \
SPEC_OPTS="-e \"should be successful and return 3 items\""
Run Code Online (Sandbox Code Playgroud)
Dou*_*rer 58
您可以将正则表达式传递给spec命令,该命令仅运行it与您提供的名称匹配的块.
spec path/to/my_spec.rb -e "should be the correct answer"
Run Code Online (Sandbox Code Playgroud)
Mic*_*ant 25
有很多选择:
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
Run Code Online (Sandbox Code Playgroud)
Gly*_*yph 23
我运行特定测试的首选方法略有不同 - 我添加了行
RSpec.configure do |config|
config.filter_run :focus => true
config.run_all_when_everything_filtered = true
end
Run Code Online (Sandbox Code Playgroud)
到我的spec_helper文件.
现在,每当我想运行一个特定的测试(或上下文或规范)时,我只需将标签"focus"添加到它,并正常运行我的测试 - 只有焦点测试才会运行.如果我删除所有焦点标签,则run_all_when_everything_filtered启动并正常运行所有测试.
它不像命令行选项那么快速和简单 - 它确实需要您编辑要运行的测试的文件.但我觉得它让你有更多的控制权.
All*_*son 11
从项目的根目录运行命令:
# 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
Run Code Online (Sandbox Code Playgroud)
此外,您可以使用--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
Run Code Online (Sandbox Code Playgroud)
这将运行嵌套在块内的所有测试,其标签与示例选项接收的字符串参数相匹配。
使用示例选项时,我建议还将--format documentation(简写:)附加-f documentation到您的捆绑命令(例如,bundle exec rspec spec/some_file.rb -e spaghetti -f documentation)。文档格式将正常./F输出替换为易于阅读的漂亮打印细目,显示您正在运行的示例的嵌套块标签,并it以绿色或红色输出每个示例(块)的打印标签以指示它是否通过或失败。这可以更好地确认您的示例参数与您打算运行的规范相匹配,并且可以实时查看在较长的测试运行期间哪些示例通过/失败,其中示例参数匹配许多块标签和/或匹配的块包含许多嵌套示例。
附加阅读(文档链接)
@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,我们可以简单地执行rspec spec/unit/baseball_spec.rb[1:1]或rspec spec/unit/baseball_spec.rb[1:1:1]取决于测试用例的嵌套方式,而不是
在第一次测试中进行测试(第42行
测试).
小智 9
不确定有多少时间可用,但是有用于运行筛选的Rspec配置-现在,您可以将其添加到您的spec_helper.rb:
RSpec.configure do |config|
config.filter_run_when_matching :focus
end
Run Code Online (Sandbox Code Playgroud)
然后将焦点标签添加到it,context或describe仅运行该代码块:
it 'runs a test', :focus do
...test code
end
Run Code Online (Sandbox Code Playgroud)
RSpec文档:
小智 9
对于spec文件的单个示例,您需要在最后添加行号,例如
rspec spec/controllers/api/v1/card_list_controller_spec.rb:35
Run Code Online (Sandbox Code Playgroud)
对于单个文件,您可以指定文件路径,例如
rspec spec/controllers/api/v1/card_list_controller_spec.rb
Run Code Online (Sandbox Code Playgroud)
对于 spec 文件夹中的 Whole Rspec Example,您可以尝试使用此命令
bundle exec rspec spec
Run Code Online (Sandbox Code Playgroud)
对于模型,它只会在第 5 行上运行案例
bundle exec rspec spec/models/user_spec.rb:5
Run Code Online (Sandbox Code Playgroud)
对于控制器:它只会在第 5 行上运行 case
bundle exec rspec spec/controllers/users_controller_spec.rb:5
Run Code Online (Sandbox Code Playgroud)
对于信号模型或控制器,从上面删除行号
在所有模型上运行案例
bundle exec rspec spec/models
Run Code Online (Sandbox Code Playgroud)
在所有控制器上运行案例
bundle exec rspec spec/controllers
Run Code Online (Sandbox Code Playgroud)
运行所有案例
bundle exec rspec
Run Code Online (Sandbox Code Playgroud)
我使用这种方式来运行单个测试文件(所有测试都在一个文件中)
rails test -n /TopicsControllerTest/ -v
Run Code Online (Sandbox Code Playgroud)
类名可用于匹配所需的文件 TopicsControllerTest
我的课 class TopicsControllerTest < ActionDispatch::IntegrationTest
输出:
如果您愿意,可以调整正则表达式以匹配单个测试方法 \TopicsControllerTest#test_Should_delete\
rails test -n /TopicsControllerTest#test_Should_delete/ -v
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
153002 次 |
| 最近记录: |