Skip to content

Commit

Permalink
update some features
Browse files Browse the repository at this point in the history
  • Loading branch information
dchelimsky committed Oct 30, 2010
1 parent 929c9ad commit 3e10475
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 75 deletions.
95 changes: 51 additions & 44 deletions features/hooks/around_hooks.feature
@@ -1,61 +1,68 @@
Feature: around hooks

As a developer using RSpec
I want to run the examples as part of a block given to a an arbitary function
So that I can control the environment in which it is run
Around hooks receive the example as a block argument, extended to behave like
a proc. This lets you define code that should be executed before and after
the example. Of course, you can do the same thing with before and after hooks,
and it's often cleaner to do so.

Scenario: around hooks defined in a group are run
Given a file named "ensure_around_blocks_are_run.rb" with:
Where around hooks shine is when you want to run an example in a block. For
example, if your database library offers a transaction method that receives
a block, you can use an around hook as described in the first scenario:

Scenario: use the example as a block within the block passed to around()
Given a file named "example_spec.rb" with:
"""
class Database
def self.transaction
puts "open transaction"
yield
puts "close transaction"
end
end
describe "around filter" do
around(:each) do |example|
puts "around each before"
example.run
puts "around each after"
Database.transaction(&example)
end
it "gets run in order" do
puts "in the example"
puts "run the example"
end
end
"""
When I run "rspec ./ensure_around_blocks_are_run.rb"
When I run "rspec example_spec.rb"
Then the output should contain:
"""
around each before
in the example
around each after
open transaction
run the example
close transaction
"""

Scenario: argument passed to around hook can be treated as a proc
Given a file named "treat_around_hook_arg_as_a_proc.rb" with:
Scenario: invoke the example using run()
Given a file named "example_spec.rb" with:
"""
describe "around filter" do
def perform_around
around(:each) do |example|
puts "around each before"
yield
example.run
puts "around each after"
end
around(:each) do |example|
perform_around(&example)
end
it "gets run in order" do
puts "in the example"
end
end
"""
When I run "rspec ./treat_around_hook_arg_as_a_proc.rb"
When I run "rspec example_spec.rb"
Then the output should contain:
"""
around each before
in the example
around each after
"""

Scenario: around hooks defined globally are run
Given a file named "ensure_around_blocks_are_run.rb" with:
Scenario: define a global around hook
Given a file named "example_spec.rb" with:
"""
RSpec.configure do |c|
c.around(:each) do |example|
Expand All @@ -71,7 +78,7 @@ Feature: around hooks
end
end
"""
When I run "rspec ./ensure_around_blocks_are_run.rb"
When I run "rspec example_spec.rb"
Then the output should contain:
"""
around each before
Expand All @@ -80,7 +87,7 @@ Feature: around hooks
"""

Scenario: before/after(:each) hooks are wrapped by the around hook
Given a file named "ensure_around_blocks_are_run.rb" with:
Given a file named "example_spec.rb" with:
"""
describe "around filter" do
around(:each) do |example|
Expand All @@ -102,7 +109,7 @@ Feature: around hooks
end
end
"""
When I run "rspec ./ensure_around_blocks_are_run.rb"
When I run "rspec example_spec.rb"
Then the output should contain:
"""
around each before
Expand All @@ -113,7 +120,7 @@ Feature: around hooks
"""

Scenario: before/after(:all) hooks are NOT wrapped by the around hook
Given a file named "ensure_around_blocks_are_run.rb" with:
Given a file named "example_spec.rb" with:
"""
describe "around filter" do
around(:each) do |example|
Expand All @@ -135,7 +142,7 @@ Feature: around hooks
end
end
"""
When I run "rspec ./ensure_around_blocks_are_run.rb"
When I run "rspec example_spec.rb"
Then the output should contain:
"""
before all
Expand All @@ -145,8 +152,8 @@ Feature: around hooks
.after all
"""

Scenario: examples run by an around block should run in the configured context
Given a file named "around_block_with_context.rb" with:
Scenario: examples run by an around block are run in the configured context
Given a file named "example_spec.rb" with:
"""
module IncludedInConfigureBlock
def included_in_configure_block; true; end
Expand All @@ -166,11 +173,11 @@ Feature: around hooks
end
end
"""
When I run "rspec ./around_block_with_context.rb"
When I run "rspec example_spec.rb"
Then the output should contain "1 example, 0 failure"

Scenario: implicitly pending examples should be detected as Not Yet Implemented
Given a file named "around_block_with_implicit_pending_example.rb" with:
Scenario: implicitly pending examples are detected as Not Yet Implemented
Given a file named "example_spec.rb" with:
"""
describe "implicit pending example" do
around(:each) do |example|
Expand All @@ -180,7 +187,7 @@ Feature: around hooks
it "should be detected as Not Yet Implemented"
end
"""
When I run "rspec ./around_block_with_implicit_pending_example.rb"
When I run "rspec example_spec.rb"
Then the output should contain "1 example, 0 failures, 1 pending"
And the output should contain:
"""
Expand All @@ -190,8 +197,8 @@ Feature: around hooks
"""


Scenario: explicitly pending examples should be detected as pending
Given a file named "around_block_with_explicit_pending_example.rb" with:
Scenario: explicitly pending examples are detected as pending
Given a file named "example_spec.rb" with:
"""
describe "explicit pending example" do
around(:each) do |example|
Expand All @@ -203,16 +210,16 @@ Feature: around hooks
end
end
"""
When I run "rspec ./around_block_with_explicit_pending_example.rb"
When I run "rspec example_spec.rb"
Then the output should contain "1 example, 0 failures, 1 pending"
And the output should contain:
"""
explicit pending example should be detected as pending
# No reason given
"""

Scenario: multiple around hooks in the same scope are all run
Given a file named "around_hooks_in_same_scope.rb" with:
Scenario: multiple around hooks in the same scope
Given a file named "example_spec.rb" with:
"""
describe "if there are multiple around hooks in the same scope" do
around(:each) do |example|
Expand All @@ -233,7 +240,7 @@ Feature: around hooks
end
end
"""
When I run "rspec ./around_hooks_in_same_scope.rb"
When I run "rspec example_spec.rb"
Then the output should contain "1 example, 0 failure"
And the output should contain:
"""
Expand All @@ -244,8 +251,8 @@ Feature: around hooks
first around hook after
"""

Scenario: around hooks in outer scopes are run
Given a file named "around_hooks_in_outer_scope.rb" with:
Scenario: around hooks in multiple scopes
Given a file named "example_spec.rb" with:
"""
describe "if there are around hooks in an outer scope" do
around(:each) do |example|
Expand Down Expand Up @@ -293,7 +300,7 @@ Feature: around hooks
end
end
"""
When I run "rspec ./around_hooks_in_outer_scope.rb"
When I run "rspec example_spec.rb"
Then the output should contain "1 example, 0 failure"
And the output should contain:
"""
Expand Down
26 changes: 0 additions & 26 deletions features/hooks/halt.feature

This file was deleted.

3 changes: 3 additions & 0 deletions features/metadata/described_class.feature
@@ -1,5 +1,8 @@
Feature: described class

If the first argument to the outermost example group is a class, the class is
exposed to each example via the described_class() method.

Scenario: access the described class from the example
Given a file named "spec/example_spec.rb" with:
"""
Expand Down
3 changes: 2 additions & 1 deletion features/subject/explicit_subject.feature
@@ -1,6 +1,7 @@
Feature: explicit subject

You can override the implicit subject using the subject() method.
Use subject() in the group scope to explicitly define the value that is
returned by the subject() method in the example scope.

Scenario: subject in top level group
Given a file named "top_level_subject_spec.rb" with:
Expand Down
7 changes: 3 additions & 4 deletions features/subject/implicit_subject.feature
@@ -1,9 +1,8 @@
Feature: implicit subject

If the first argument to the outermost example group is a class, an
instance of that class is made available to each example as the
implicit subject of that example.

If the first argument to the outermost example group is a class, an instance
of that class is exposed to each example via the subject() method.

Scenario: subject in top level group
Given a file named "top_level_subject_spec.rb" with:
"""
Expand Down

0 comments on commit 3e10475

Please sign in to comment.