【问题标题】:Rspec - How to create macros with tags?Rspec - 如何使用标签创建宏?
【发布时间】:2016-02-18 16:04:30
【问题描述】:

有没有办法有条件地运行 Rspec 宏?

例如使用过滤宏的能力

RSpec.configure do |c|
  c.filter_run_excluding :broken => true
end

## This should get skipped
it_should_validate_with_macro :some_param, :broken => true

注意:这是动态调用一组测试。所以pending 不是我想要的解决方案。

【问题讨论】:

    标签: ruby-on-rails rspec rspec2 rspec3


    【解决方案1】:
    describe "an example" do
      it "is implemented but waiting" do
        pending("something else getting finished")
        this_should_not_get_executed
      end
    end
    

    来自https://relishapp.com/rspec/rspec-core/v/2-0/docs/pending/pending-examples

    【讨论】:

    • 是的,我知道pending。但我想动态调用一组测试。所以这行不通。
    【解决方案2】:

    您可以在同一进程中运行规范,也可以执行capture the output 之类的操作。

    但一个可能对您有用的简单示例就是创建一个 ruby​​ 脚本:

    require 'rspec/core'
    
    RSpec.configuration.add_setting(:some_setting)
    RSpec.configuration.some_setting = false
    RSpec::Core::Runner.run(['spec/models/bar.rb'])
    RSpec.clear_examples
    if RSpec.configuration.some_setting
      RSpec::Core::Runner.run(['spec/models/foo.rb'])
    end
    

    然后,在您的 rspec 脚本中修改设置:

    RSpec.describe 'bar' do
      it 'bar' do
        RSpec.configuration.some_setting = true
      end
    end
    

    这将有条件地运行 foo.rb 中的规范。

    【讨论】:

      猜你喜欢
      • 2021-01-07
      • 1970-01-01
      • 2021-12-03
      • 2015-05-11
      • 2019-10-20
      • 2021-08-25
      • 2017-03-13
      • 1970-01-01
      相关资源
      最近更新 更多