Rspec-core: Multiple types of specs in metadata

Created on 24 Oct 2016  ·  3Comments  ·  Source: rspec/rspec-core

I'd like to define multiple types of specs for metadata, but it isn't currently possible.

I'd like to define things like:

config.when_first_matching_example_defined(types: %i(feature request)) do
  # enable db support only for feature or request specs
  require 'support/db'
end

# include these Helpers only into feature or request specs
config.include(Helpers, types: %i(feature request))

Currently I have to duplicate the definitions.

config.when_first_matching_example_defined(types: :feature) do
  require 'support/db'
end

config.when_first_matching_example_defined(types: :request) do
  require 'support/db'
end

config.include(Helpers, type: :feature)
config.include(Helpers, type: :request)

Most helpful comment

I don't think that above solution improves readability and in spec/rails_helper. Some one-liner solution would be great for these cases. I don't think, I'm alone who would appreciate something what I proposed.

All 3 comments

You can do this:

%i(feature request).each do |type|
  config.when_first_matching_example_defined(type: type) do
    # enable db support only for feature or request specs
    require 'support/db'
  end

  # include these Helpers only into feature or request specs
  config.include(Helpers, type: type)
end

Given how easy it is to do with a simple loop, I don't see a reason for us to add support for what you're asking for--particularly since it would be a very implicit, confusing way for metadata to match, IMO.

I don't think that above solution improves readability and in spec/rails_helper. Some one-liner solution would be great for these cases. I don't think, I'm alone who would appreciate something what I proposed.

Given that currently we treat metadata values as simple values, it would really be confusing to start giving them special meaning, not to mention ambiguous!

Was this page helpful?
0 / 5 - 0 ratings