【问题标题】:How to learn spec type from helper method?如何从辅助方法中学习规范类型?
【发布时间】:2015-10-27 03:20:10
【问题描述】:

我正在从我的小网站编写规范,但遇到了一个小问题。

我有一些规范辅助方法,例如:

  def is_logged_in?(spec = :feature)
    if spec == :feature
      page.has_selector?('#user-menu')
    else
      !session[:user_id].nil?
    end
  end

但是我厌倦了传递“spec”参数,我认为有更好的解决方案,比如:if current_spec.type == :feature ...

有没有办法从辅助方法中了解当前运行规范的类型?

【问题讨论】:

    标签: ruby-on-rails rspec tdd integration-testing bdd


    【解决方案1】:

    您可以使用以下方式访问 rspec 测试的类型:

    example.metadata[:type]
    # => :controller # in case of a controller spec
    

    请注意,example 仅适用于示例组(例如 describecontext 块)。 example 不适用于示例(例如 it 块)或在示例范围内运行的构造(例如 beforelet 等)。

    【讨论】:

    • 嗯...我需要从it块访问...在示例组中它对我来说有点没用...
    • 不幸的是,rspec 不支持。您无法从 it 块访问它。
    • 不客气。我认为你必须想出其他方法。总有另一种方式,更好的方式:)
    【解决方案2】:

    好的,我想出了如何区分功能规范和其他规范:

    def feature_test?
      defined?(visit)
    end
    

    它非常适合我的代码 =-)

    【讨论】:

      猜你喜欢
      • 2014-03-24
      • 1970-01-01
      • 2021-05-20
      • 2019-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多