如何从辅助方法中学习规范类型?

How to learn spec type from helper method?

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

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

  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 ....

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

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

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

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

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

def feature_test?
  defined?(visit)
end

它很适合我的代码 =-)