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 -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20120401/808c472e/attachment.html>
On Sun, Apr 1, 2012 at 11:34 PM, Bradley <bradleyrobertson at gmail.com> wrote:> 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.There is a documented way to declare a module with let and before declarations: http://rubydoc.info/gems/rspec-core/RSpec/Core/SharedContext. There is also a documented way to include a module in every ExampleGroup: http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration#include-instance_method There is, admittedly, no doc that says you can use these two features together. Obviously we can''t do that for every pair of features, but perhaps this pair is worth citing specifically. I''ll look into doing that for the next release. Cheers, David
dchelimsky at gmail.com
2012-Apr-27 02:43 UTC
[rspec-users] using shared_context in a global way
On Sunday, April 1, 2012 9:34:00 PM UTC-5, Bradley wrote:> > 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 >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 [1] http://rubydoc.info/gems/rspec-core/RSpec#configure-class_method [2] http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration [3] http://rubydoc.info/gems/rspec-core/RSpec/Core/SharedContext -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20120426/2aae9a48/attachment-0001.html>