-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core] Merge pull request rspec/rspec-core#2895 from odinhb/current_s…
…cope Add RSpec.current_scope method to replace `currently_executing_a_context_hook?` and `self.inspect` hack --- This commit was imported from rspec/rspec-core@07db8f8.
- Loading branch information
Showing
8 changed files
with
138 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
Feature: RSpec provides the current scope as RSpec.current_scope | ||
|
||
You can detect which rspec scope your helper methods or library code is executing in. | ||
This is useful if for example, your method only makes sense to call in a certain context. | ||
|
||
Scenario: Detecting the current scope | ||
Given a file named "current_scope_spec.rb" with: | ||
"""ruby | ||
# Outside of the test lifecycle, the current scope is `:suite` | ||
exit(1) unless RSpec.current_scope == :suite | ||
at_exit do | ||
exit(1) unless RSpec.current_scope == :suite | ||
end | ||
RSpec.configure do |c| | ||
c.before :suite do | ||
expect(RSpec.current_scope).to eq(:before_suite_hook) | ||
end | ||
c.before :context do | ||
expect(RSpec.current_scope).to eq(:before_context_hook) | ||
end | ||
c.before :example do | ||
expect(RSpec.current_scope).to eq(:before_example_hook) | ||
end | ||
c.around :example do |ex| | ||
expect(RSpec.current_scope).to eq(:before_example_hook) | ||
ex.run | ||
expect(RSpec.current_scope).to eq(:after_example_hook) | ||
end | ||
c.after :example do | ||
expect(RSpec.current_scope).to eq(:after_example_hook) | ||
end | ||
c.after :context do | ||
expect(RSpec.current_scope).to eq(:after_context_hook) | ||
end | ||
c.after :suite do | ||
expect(RSpec.current_scope).to eq(:after_suite_hook) | ||
end | ||
end | ||
RSpec.describe "RSpec.current_scope" do | ||
before :context do | ||
expect(RSpec.current_scope).to eq(:before_context_hook) | ||
end | ||
before :example do | ||
expect(RSpec.current_scope).to eq(:before_example_hook) | ||
end | ||
around :example do |ex| | ||
expect(RSpec.current_scope).to eq(:before_example_hook) | ||
ex.run | ||
expect(RSpec.current_scope).to eq(:after_example_hook) | ||
end | ||
after :example do | ||
expect(RSpec.current_scope).to eq(:after_example_hook) | ||
end | ||
after :context do | ||
expect(RSpec.current_scope).to eq(:after_context_hook) | ||
end | ||
it "is :example in an example" do | ||
expect(RSpec.current_scope).to eq(:example) | ||
end | ||
it "works for multiple examples" do | ||
expect(RSpec.current_scope).to eq(:example) | ||
end | ||
describe "in nested describe blocks" do | ||
it "still works" do | ||
expect(RSpec.current_scope).to eq(:example) | ||
end | ||
end | ||
end | ||
""" | ||
When I run `rspec current_scope_spec.rb` | ||
Then the examples should all pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters