rspec before(:each) hook - conditionally apply

╄→гoц情女王★ 提交于 2019-12-25 04:29:15

问题


I have following in my rails_helper.rb:

RSpec.configure do |config|
  # ...
  config.before(:each, type: :controller) do
    # SOMETHING
  end
end

I want to define directories, to which this SOMETHING will be applicable (in my case ONLY to files under spec/controllers/api directory).

Any chance to achieve that?


回答1:


You can use a more specialized name for your RSpec filter:

RSpec.configure do |config|
  # ...
  config.before(:each, :subtype => :controllers_api) do
    # SOMETHING
  end
end

And then in your RSpec examples in spec/controllers/api, you add some metadata:

RSpec.describe "something", :subtype => :controllers_api do
end

This SOMETHING should run only on examples having the :subtype => :controllers_api metadata. I did not use :type because there may be other behavior defined with it.



来源:https://stackoverflow.com/questions/29094646/rspec-beforeeach-hook-conditionally-apply

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!