Skip to content

Instantly share code, notes, and snippets.

@dchelimsky
Created March 6, 2011 00:04
Show Gist options
  • Save dchelimsky/856845 to your computer and use it in GitHub Desktop.
Save dchelimsky/856845 to your computer and use it in GitHub Desktop.
shared_thingy("string"){ ... }
shared_thingy(:symbol){ ... }
shared_thingy(:key => :value){ ... }
describe "something", :key => :value do
# ^^ evals the block in the host group
describe "something", :symbol do
# ^^ evals the block in the host group if treating symbols as metadata
describe "something" do
with_shared_context "string"
# ^^ evals the block in the host group
describe "something" do
it_behaves_like "string"
# ^^ generates a nested group and evals the block in that context
@myronmarston
Copy link

Instead, how about this?

shared_context "logged in" do
  # before hook that logs in
end

shared_context "logged in as" do |user|
  # before hook to log in as the given user
end

RSpec.configure do |c|
  c.include_shared_context "logged in", :logged_in => true
end

describe "something" do
  with_shared_context "logged in"
end

describe "something else", :logged_in => true do
end

describe "a third thing" do
  with_shared_context "logged in as", "[email protected]"
end

Benefits, as I see them:

  • The declaration of a shared context mirrors the declaration of a shared example group.
  • You can use shared contexts in the same way you can use shared example groups (even with arguments!).
  • You can leverage metadata to include a shared context using configuration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment