Skip to content

Instantly share code, notes, and snippets.

@bogdanRada
Forked from myronmarston/LICENSE
Created February 7, 2013 15:47
Show Gist options
  • Save bogdanRada/4731770 to your computer and use it in GitHub Desktop.
Save bogdanRada/4731770 to your computer and use it in GitHub Desktop.

RSpec Around All

Provides around(:all) hooks for RSpec.

Usage

In your Gemfile:

gem 'rspec_around_all', git: 'git://gist.github.com/2005175.git'

In a spec:

require 'rspec_around_all'

describe "MyClass" do
  around(:all) do |group|
    # do something before
    group.run_examples
    # do something after
  end

  # or...
  around(:all) do |group|
    transactionally(&group)
  end
end

You may want to check out my blog post about this.

Gem::Specification.new do |s|
s.name = 'rspec_around_all'
s.version = '0.1.0'
s.platform = Gem::Platform::RUBY
s.author = 'Myron Marston'
s.email = 'myron.marston@gmail.com'
s.summary = 'Provides around(:all) hook for RSpec'
s.files = ['rspec_around_all.rb']
s.test_file = 'rspec_around_all_spec.rb'
s.require_path = '.'
s.add_dependency 'rspec', "~> 2.0"
s.required_ruby_version = '>= 1.9.2'
end
require 'delegate'
require 'fiber'
module RSpecAroundAll
class FiberAwareGroup < SimpleDelegator
def run_examples
Fiber.yield
end
def to_proc
proc { run_examples }
end
end
def around(scope, &block)
# let RSpec handle around(:each) hooks...
return super(scope, &block) unless scope == :all
group, fiber = self, nil
before(:all) do
fiber = Fiber.new(&block)
fiber.resume(FiberAwareGroup.new(group))
end
after(:all) do
fiber.resume
end
end
end
RSpec.configure do |c|
c.extend RSpecAroundAll
end
require_relative 'rspec_around_all'
module RSpec
module Core
describe "around(:all) hook" do
it "runs the hook around all examples" do
order = []
group = ExampleGroup.describe "group" do
around(:all) do |g|
order << :before
g.run_examples
order << :after
end
specify { order << :e1 }
specify { order << :e2 }
end
group.run(stub.as_null_object)
order.should eq([:before, :e1, :e2, :after])
end
it 'allows the yielded arg to be treated as a proc' do
group = ExampleGroup.describe "group" do
def self.order
@order ||= []
end
def self.transactionally
order << :before
yield
order << :after
end
around(:all) { |g| transactionally(&g) }
specify { self.class.order << :e1 }
specify { self.class.order << :e2 }
end
group.run(stub.as_null_object)
group.order.should eq([:before, :e1, :e2, :after])
end
it 'can access metadata in the hook' do
foo_value = nil
group = ExampleGroup.describe "group", :foo => :bar do
around(:all) do |group|
foo_value = group.metadata[:foo]
group.run_examples
end
specify { }
end
group.run(stub.as_null_object)
foo_value.should eq(:bar)
end
it 'allows around(:each) hooks to run as normal' do
order = []
group = ExampleGroup.describe "group" do
around(:each) do |e|
order << :before
e.run
order << :after
end
specify { order << :e1 }
specify { order << :e2 }
end
group.run(stub.as_null_object)
order.should eq([:before, :e1, :after, :before, :e2, :after])
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment