Instantly share code, notes, and snippets.

Embed
What would you like to do?

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.

Copyright

Copyright (c) 2012-2013 Myron Marston. Released under the terms of the MIT license. See LICENSE for details.

(The MIT License)
Copyright (c) 2012-2013 Myron Marston
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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.license = 'MIT'
s.files = ['rspec_around_all.rb', 'LICENSE']
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