Skip to content

Instantly share code, notes, and snippets.

@myronmarston
Last active October 29, 2022 21:04
Show Gist options
  • Save myronmarston/2005175 to your computer and use it in GitHub Desktop.
Save myronmarston/2005175 to your computer and use it in GitHub Desktop.

RSpec Around All/Context

Provides around(:all)/around(:context) hooks for RSpec.

Usage

In your Gemfile:

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

In a spec:

require 'rspec_around_context'

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

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

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

Copyright

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

(The MIT License)
Copyright (c) 2012-2020 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_context'
s.version = '0.2.0'
s.platform = Gem::Platform::RUBY
s.author = 'Myron Marston'
s.email = '[email protected]'
s.summary = 'Provides around(:context) hook for RSpec'
s.license = 'MIT'
s.files = ['rspec_around_context.rb', 'LICENSE']
s.test_file = 'rspec_around_context_spec.rb'
s.require_path = '.'
s.add_dependency 'rspec', "~> 3.0"
s.required_ruby_version = '>= 1.9.2'
end
require 'delegate'
require 'fiber'
module RSpecAroundContext
class FiberAwareGroup < SimpleDelegator
def run_examples
Fiber.yield
end
def to_proc
proc { run_examples }
end
end
def around(scope, *args, &block)
# let RSpec handle around(:each) hooks...
return super(scope, *args, &block) unless scope == :all || scope == :context
group, fiber = self, nil
before(scope, *args) do
fiber = Fiber.new(&block)
fiber.resume(FiberAwareGroup.new(group))
end
after(scope, *args) do
fiber.resume
end
end
end
RSpec.configure do |c|
c.extend RSpecAroundContext
end
require_relative 'rspec_around_context'
module RSpec
module Core
%i[ all context ].each do |scope|
describe "around(:#{scope}) hook" do
it "runs the hook around all examples" do
order = []
group = ExampleGroup.describe "group" do
around(scope) do |g|
order << :before
g.run_examples
order << :after
end
specify { order << :e1 }
specify { order << :e2 }
end
group.run(double.as_null_object)
expect(order).to 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(scope) { |g| transactionally(&g) }
specify { self.class.order << :e1 }
specify { self.class.order << :e2 }
end
group.run(double.as_null_object)
expect(group.order).to 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(scope) do |group|
foo_value = group.metadata[:foo]
group.run_examples
end
specify { }
end
group.run(double.as_null_object)
expect(foo_value).to 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(double.as_null_object)
expect(order).to eq([:before, :e1, :after, :before, :e2, :after])
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment