【问题标题】:How to run a single test in guard for rspec?如何在 rspec 中运行单个测试?
【发布时间】:2014-02-03 07:53:15
【问题描述】:

我使用guard-rspec 在我的文件更改时自动运行必要的 rspec 测试,我喜欢它的工作原理。然而,当我调试一个包含多个测试的文件时,有时我只想重新运行一个单独的测试。例如,使用命令行中的 rspec:

rspec spec/requests/my_favorite_spec.rb:100

这将只运行my_favorite_spec.rb 中第 100 行的单个规范。

我尝试将上述内容输入到守卫控制台,但它只是运行了所有测试,就好像我刚刚按下了回车键一样。守卫控制台中是否有另一种语法来运行单个规范?

【问题讨论】:

    标签: ruby-on-rails ruby rspec guard


    【解决方案1】:

    我认为您可以为要运行的规范添加“focus: true”选项,例如

    it 'does something', focus: true do
      //your spec
    end
    

    然后你保存文件,然后守卫只运行那个专注的测试

    完成后,您只需删除“focus: true”

    【讨论】:

      【解决方案2】:

      您必须为您的spec/spec_helper.rb 文件提供参数以接受:focus => true 声明。

      RSpec.configure do |config|
        config.filter_run :focus => true
      end
      

      然后就可以使用了

      it 'does something', :focus => true do
        //your spec
      end
      

      describe "something", :focus => true do
        before do
           sign in
           visit page
        end
      
        it { does something }
        it { does something else }
      end
      

      如果您打算采用这种方法,您可能还想确保在任何地方都没有:focus => true 时运行所有规范,使用documented approach

      RSpec.configure do |config|
        config.run_all_when_everything_filtered = true
      end
      

      您可以使用过滤器做很多事情;你可能想看看这个页面:https://relishapp.com/rspec/rspec-core/v/3-0/docs/filtering

      【讨论】:

      • 这正是我所需要的,而且链接也很有帮助。谢谢!
      • 这行得通,但是当我从it 块中删除:focus => true 时,guardAll examples were filtered out。显然这不能满足明显的工作流程。如果不存在 :focus 子句,则完整的解决方案将运行所有示例。
      • 好的,当没有:focus 子句时,您可以运行所有规范,通过config.run_all_when_everything_filtered = true 块中的config.run_all_when_everything_filtered = true。这里是the docs
      • 有没有办法从命令行做到这一点?我正在尝试使命令行功能同时适用于警卫和并行规范
      【解决方案3】:

      您可以在 Guardfile 中使用 failed_mode: :focus,如下所述:https://github.com/guard/guard-rspec#list-of-available-options

      【讨论】:

        猜你喜欢
        • 2014-11-27
        • 2012-11-28
        • 2010-09-13
        • 1970-01-01
        • 2011-07-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-03
        • 1970-01-01
        相关资源
        最近更新 更多