如何运行单个RSpec测试?
我有以下文件:
/spec/controllers/groups_controller_spec.rb
我在终端中使用什么命令来运行该规范以及在哪个目录中运行命令?
我的gem文件:
# 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/controllers/groups_controller_spec.rb:42
其中42
表示我想要运行的测试行。
EDIT1:
你也可以使用标签。 看到这里 。
编辑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命令,该命令仅运行与您提供的名称匹配的块。
spec path/to/my_spec.rb -e "should be the correct answer"
我运行特定测试的首选方法略有不同 – 我添加了行
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 # 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
@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
输出:
如果你想要你可以调整正则表达式以匹配单个测试方法\TopicsControllerTest#test_Should_delete\
rails test -n /TopicsControllerTest#test_Should_delete/ -v
从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
对于型号,它仅在第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
鉴于你在使用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’命令。
我使用这个guard gem来自动运行我的测试。 它在测试文件上创建或更新操作后执行测试。
https://github.com/guard/guard-test
或者通常可以使用以下命令运行
rspec spec / controllers / 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