-
-
Notifications
You must be signed in to change notification settings - Fork 753
Shared example group inclusion changes #2256
Changes from all commits
157e20f
fe14f85
9795a57
6e5a48f
86362bf
9ca21ab
ed2d59c
3589ab5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,23 @@ | ||
Feature: shared context | ||
|
||
Use `shared_context` to define a block that will be evaluated in the context | ||
of example groups either explicitly, using `include_context`, or implicitly by | ||
matching metadata. | ||
Use `shared_context` to define a block that will be evaluated in the context of example groups either locally, using `include_context` in an example group, or globally using `config.include_context`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the non broken up line for relish formatting? It's a real shame there isn't a better way... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep. If we put line breaks, it'll render with line breaks on relish even if that's not a good place for it.__ |
||
|
||
When implicitly including shared contexts via matching metadata, the normal way is to define matching metadata on an example group, in which case the context is included in the entire group. However, you also have the option to include it in an individual example instead. RSpec treats every example as having a singleton example group (analogous to Ruby's singleton classes) containing just the one example. | ||
|
||
Background: | ||
Given a file named "shared_stuff.rb" with: | ||
"""ruby | ||
RSpec.shared_context "shared stuff", :a => :b do | ||
RSpec.configure do |rspec| | ||
# This config option will be enabled by default on RSpec 4, | ||
# but for reasons of backwards compatibility, you have to | ||
# set it on RSpec 3. | ||
# | ||
# It causes the host group and examples to inherit metadata | ||
# from the shared context. | ||
rspec.shared_context_metadata_behavior = :apply_to_host_groups | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will need to go in Sam's blog post (something about this that is) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, planning on it :). |
||
|
||
RSpec.shared_context "shared stuff", :shared_context => :metadata do | ||
before { @some_var = :some_value } | ||
def shared_method | ||
"it works" | ||
|
@@ -19,6 +27,10 @@ Feature: shared context | |
'this is the subject' | ||
end | ||
end | ||
|
||
RSpec.configure do |rspec| | ||
rspec.include_context "shared stuff", :include_shared => true | ||
end | ||
""" | ||
|
||
Scenario: Declare a shared context and include it with `include_context` | ||
|
@@ -44,6 +56,13 @@ Feature: shared context | |
it "accesses the subject defined in the shared context" do | ||
expect(subject).to eq('this is the subject') | ||
end | ||
|
||
group = self | ||
|
||
it "inherits metadata from the included context" do |ex| | ||
expect(group.metadata).to include(:shared_context => :metadata) | ||
expect(ex.metadata).to include(:shared_context => :metadata) | ||
end | ||
end | ||
""" | ||
When I run `rspec shared_context_example.rb` | ||
|
@@ -72,7 +91,7 @@ Feature: shared context | |
"""ruby | ||
require "./shared_stuff.rb" | ||
|
||
RSpec.describe "group that includes a shared context using metadata", :a => :b do | ||
RSpec.describe "group that includes a shared context using metadata", :include_shared => true do | ||
it "has access to methods defined in shared context" do | ||
expect(shared_method).to eq("it works") | ||
end | ||
|
@@ -88,6 +107,13 @@ Feature: shared context | |
it "accesses the subject defined in the shared context" do | ||
expect(subject).to eq('this is the subject') | ||
end | ||
|
||
group = self | ||
|
||
it "inherits metadata from the included context" do |ex| | ||
expect(group.metadata).to include(:shared_context => :metadata) | ||
expect(ex.metadata).to include(:shared_context => :metadata) | ||
end | ||
end | ||
""" | ||
When I run `rspec shared_context_example.rb` | ||
|
@@ -103,9 +129,13 @@ Feature: shared context | |
expect(self).not_to respond_to(:shared_method) | ||
end | ||
|
||
it "has access to shared methods from examples with matching metadata", :a => :b do | ||
it "has access to shared methods from examples with matching metadata", :include_shared => true do | ||
expect(shared_method).to eq("it works") | ||
end | ||
|
||
it "inherits metadata form the included context due to the matching metadata", :include_shared => true do |ex| | ||
expect(ex.metadata).to include(:shared_context => :metadata) | ||
end | ||
end | ||
""" | ||
When I run `rspec shared_context_example.rb` | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Woop :)