问题
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