rspec/rspec-core

Feature request: include shared context based on class

Closed this issue · 1 comments

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

👋 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.