using shared_context in a global way

30 views
Skip to first unread message

Bradley

unread,
Apr 1, 2012, 10:34:00 PM4/1/12
to rs...@googlegroups.com
I have some let and before declarations that I want to define for all of my tests.  I can't seem to figure out how to do this in a clean way (ie by including a module in my Rspec config).  I don't want to have to include_context for each test, I just want to run some before(:all) hooks and provide access to some default variables that example groups could override with their own let declarations.

I must be missing something because I can't find anywhere in the docs how I might achieve this.  Any help is greatly appreciated.

Brad

dchel...@gmail.com

unread,
Apr 26, 2012, 10:43:18 PM4/26/12
to rs...@googlegroups.com, rspec-users
Hey Brad,

You can set up global `before` hooks using `RSpec.configure` [1] [2]:

  RSpec.configure {|c| c.before(:all) { do_stuff }}

`let` is not supported in `RSpec.configure`, but you _can_ set up a global `let` by including it in a SharedContext module [3] and including that module using `config.before`:

  module MyLetDeclarations
    extend RSpec::Core::SharedContext
    let(:foo) { Foo.new }
  end
  RSpec.configure { |c| c.include MyLetDeclarations }

If you're going to use that, you may as well put all your global stuff in the same module:

  module GlobalStuff
    extend RSpec::Core::SharedContext
    before(:all) { do_stuff }
    let(:foo) { Foo.new }
  end
  RSpec.configure { |c| c.include MyLetDeclarations }

HTH,
David  

Reply all
Reply to author
Forward
0 new messages