我的rails_helper.rb
:中有以下内容
RSpec.configure do |config|
# ...
config.before(:each, type: :controller) do
# SOMETHING
end
end
我想定义目录,这个SOMETHING
将适用于这些目录(在我的情况下,仅适用于spec/controllers/api
目录下的文件)。
有机会做到这一点吗?
您可以为RSpec过滤器使用更专业的名称:
RSpec.configure do |config|
# ...
config.before(:each, subtype: :controllers_api) do
# SOMETHING
end
end
然后在spec/controllers/api
中的RSpec示例中,添加一些元数据:
RSpec.describe "something", subtype: :controllers_api do
end
该SOMETHING
将仅在具有subtype: :controllers_api
元数据的示例上运行。
要从文件位置自动派生元数据,请使用define_derived_metadata
,如下所示:
RSpec.configure do |config|
# Tag all groups and examples in the spec/controllers/api directory
# with subtype: :controllers_api
config.define_derived_metadata(file_path: %r{/spec/controllers/api}) do |metadata|
metadata[:subtype] = :controllers_api
end
end