Method: RSpec::Core::Configuration#include_context

Defined in:
lib/rspec/core/configuration.rb

#include_context(shared_group_name, *filters) ⇒ void

Note:

Filtered context inclusions can also be applied to individual examples that have matching metadata. Just like Ruby's object model is that every object has a singleton class which has only a single instance, RSpec's model is that every example has a singleton example group containing just the one example.

Tells RSpec to include the named shared example group in example groups. Use filters to constrain the groups or examples in which to include the example group.

Examples:


RSpec.shared_context "example admin user" do
  let(:admin_user) { create_user(:admin) }
end

RSpec.shared_context "example guest user" do
  let(:guest_user) { create_user(:guest) }
end

RSpec.configure do |config|
  config.include_context "example guest user", :type => :request
  config.include_context "example admin user", :admin, :type => :request
end

RSpec.describe "The admin page", :type => :request do
  it "can be viewed by admins", :admin do
     admin_user
    get "/admin"
    expect(response).to be_ok
  end

  it "cannot be viewed by guests" do
     guest_user
    get "/admin"
    expect(response).to be_forbidden
  end
end

See Also:



1482
1483
1484
1485
# File 'lib/rspec/core/configuration.rb', line 1482

def include_context(shared_group_name, *filters)
  shared_module = world.shared_example_group_registry.find([:main], shared_group_name)
  include shared_module, *filters
end