【问题标题】:Is there a way to configure guard to not run request specs?有没有办法将警卫配置为不运行请求规范?
【发布时间】:2012-03-15 19:15:47
【问题描述】:

我正在使用 guard 来运行我所有的 rails 规范,它很棒。我已经编写了一堆使用 capybara 和 selenium 通过打开 firefox 来测试我的页面 javascripts 的请求规范,它们也很棒,但是它们往往很慢,并且在我打字时将注意力从我的编辑器上移开。

有没有办法配置守卫在运行所有请求规范时不运行我的请求规范,并可能分配一个热键来运行请求规范?

【问题讨论】:

    标签: ruby-on-rails rspec guard


    【解决方案1】:

    guard-rspec 有一个 exclude 选项。它在#23 中实现,但没有在README 中记录。我做了a pull request to document that。

    示例

    guard 'rspec', :exclude => "spec/foo/**/*"  # exclude files based on glob
    

    【讨论】:

    • 而在短短 6 分钟内,它就被合并到了guard-rspec。惊人的。 :)
    • 今天守卫告诉你:“Guard::RSpec DEPRECATION WARNING: The :exclude option is deprecated. Please Guard ignore method instead.github.com/guard/guard#ignore"
    【解决方案2】:

    回答我自己的问题以防其他人遇到这个问题:

    rspec-rails 可以通过 :cli 将命令行参数传递给 rspec。此外,可以在规范文件中标记示例,然后可以运行 rspec 以包含或排除那些标记的示例。

    原来我已经用 :js=>true 标记了我想排除的示例,这就是让 Selenium 启动 Firefox 的方式。

    describe "Post" do
      it "should be able to edit a post", :js=>true do
        # your test here
      end
    end
    

    我在我的 Guardfile 中创建了两组,一组用于使用 :cli => "-t ~js" 的非 JavaScript 规范,另一组用于使用 :cli => "-t js" 测试 javascript 的规范。我还为 javascript 组传入了 :all_after_pass => false 。

    这是我的新保护文件:

    group 'none-javascript specs' do
      guard 'rspec', :version => 2, :cli => '-r rspec/instafail -f RSpec::Instafail -t ~js'  do
    
       watch(%r{^spec/.+_spec\.rb$})
       watch(%r{^lib/(.+)\.rb$})     { |m| "spec/lib/#{m[1]}_spec.rb" }
       watch('spec/spec_helper.rb')  { "spec" }
    
       # Rails example
       watch(%r{^app/(.+)\.rb$})                           { |m| "spec/#{m[1]}_spec.rb" }
       watch(%r{^app/(.*)(\.erb|\.haml|\.jbuilder)$})      { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
       watch(%r{^app/controllers/(.+)_(controller)\.rb$})  { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
       watch(%r{^spec/support/(.+)\.rb$})                  { "spec" }
       watch('config/routes.rb')                           { "spec/routing" }
       watch('app/controllers/application_controller.rb')  { "spec/controllers" }
    
       watch(%r{^app/views/(.+)/.*\.(erb|haml)$})          { |m| "spec/requests/#{m[1]}_spec.rb" }
      end
    end
    
    group 'javascript specs' do
      guard 'rspec', :version => 2, :all_after_pass => false, :cli => '-r rspec/instafail -f RSpec::Instafail -t js' do
    
        watch(%r{^app/views/(.+)/.*\.(erb|haml)$})          { |m| "spec/requests/#{m[1]}_spec.rb" }
        watch(%r{^spec/requests/.+_spec\.rb$})
      end
    end
    

    现在,当我在守卫术语中启动守卫或点击返回时,两个组都在执行所有规范。

    Guard::RSpec is running, with RSpec 2!
    Running all specs
    Run options: exclude {:js=>true}
    ...
    Finished in 75.78 seconds
    428 examples, 0 failures, 1 pending
    
    Guard::RSpec is running, with RSpec 2!
    Running all specs
    Run options: include {:js=>true}
    ...
    Finished in 63.68 seconds
    22 examples, 0 failures
    

    在所有测试通过后,仅运行非 JavaScript 示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-16
      • 1970-01-01
      • 2021-01-27
      • 1970-01-01
      • 2012-03-29
      • 2019-11-21
      相关资源
      最近更新 更多