0

I'm trying to define a few let's and before hooks that will run globally for all my specs by including them in a separate file using the Rspec configuration block.

I tried something like:

module Helpers
  def self.included(base)
    base.let(:x){ "x" }
    base.before(:all){ puts "x: #{x}" }
  end
end

Rspec.configure{|c| c.include Helpers }

but this doesn't work as expected. The before(:all) doesn't just run before each main example group, but each nested one as well.

Then I found out about shared_context and it appears to be exactly what I want.

My open problem however is that I can't figure out how to share a context amongst ALL of my specs. The docs only reference include_context within a specific spec.

Can anyone tell me how I can achieve this behavior in a global manner? I'm aware that I can define global before hooks in my spec_helper but I can't seem to use let. I'd like a single place that I can define both of these things and not pollute my spec helper, but just include it instead.

2
  • According to RSpec tests, before(:all) must run only once, no matter where it was defined. So maybe its an RSpec issue? Commented Apr 1, 2012 at 20:41
  • no i don't think this is an rspec issue. I don't think the above code is the intended usage of including modules in the config. I'm pretty sure that module will get included in every example group, including nested ones, so it makes sense that my before(:all) runs before each example group. Commented Apr 2, 2012 at 2:24

1 Answer 1

2

I tried to reproduce your error, but failed.

# spec_helper.rb
require 'support/global_helpers'

RSpec.configure do |config|
  config.include MyApp::GlobalHelpers
end

# support/global_helpers.rb
module MyApp
  module GlobalHelpers
    def self.included(base)
      base.let(:beer) { :good }
      base.before(:all) { @bottles = 10 }
    end
  end  
end

# beer_spec.rb
require 'spec_helper'

describe "Brewery" do

  it "makes good stuff" do
    beer.should be :good
  end

  it "makes not too much bottles" do
    @bottles.should == 10
  end

  context "when tasting beer" do
    before(:all) do
      @bottles -= 1
    end

    it "still produces good stuff" do
      beer.should be :good
    end

    it "spends some beer on degusting" do
      @bottles.should == 9
    end   
  end
end

https://gist.github.com/2283634

When I wrote something like base.before(:all) { p 'global before'; @bottles = 10 }, I got exactly one line in spec output.

Notice that I didn't try to modify instance variables inside an example, because it wouldn't work anyway (well, actually you can modify instance variables, if it's a hash or array). Moreover, even if you change before(:all) in nested example group to before(:each), there will be still 9 bottles in each example.

Sign up to request clarification or add additional context in comments.

2 Comments

oh interesting... i was using an old version of rspec, new version only runs once... maybe I can do what I want!
there's something still weird going on. I have a before(:all) that just does a puts statement, and it breaks my tests in unexplainable ways. Take out that before(:all) and it all works. I still don't think using a module like that is the intended method. I'm pretty sure I want a shared_context, just don't know how to use it globally.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.