rspec / rspec-core

RSpec runner and formatters
http://rspec.info
MIT License
1.23k stars 765 forks source link

Feature request: include shared context based on class #3126

Closed Jammjammjamm closed 1 week ago

Jammjammjamm commented 1 week ago

Feature request: include shared context based on class

I work on a framework which defines base classes which are then subclassed by users of the framework. Specs for these classes require identical setup. I've implemented this similar to the following:

Rspec.shared_context('when testing a framework class') do
  # shared stuff here
end

RSpec.configure do |config|
  config.define_derived_metadata do |metadata|
    if metadata[:described_class].present? && metadata[:described_class].is_a?(MyFrameworkClass)
      metadata[:framework_class] = true
    end
  end

  config.include_context 'when testing a framework class', framework_class: true
end

But it would be great to just have a filter which acts directly on the class/included modules:

config.include_context 'when testing a framework class', class: MyFrameworkClass
JonRowe commented 1 week ago

:wave: Thanks for the suggestion but this isn't something I'm interested in adding to core at this time, it seems like you have a pretty neat solution for your purposes but we prefer more explicit solutions like defining the metadata manually in the tests.