John Hinnegan
2011-Jul-29 17:36 UTC
[rspec-users] How can I make before/after all a method?
I would like to turn this: describe TestClass do before :all do # set some config end after :all do # restore some config end # do a bunch of tests to this end into describe TestClass do with_config_value(X) # do a bunch of tests to this end Basically, I want to include before :all and after :all clauses into a test. However, I do not want this to apply toa ll tests. Thanks in advance. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110729/fd399874/attachment.html>
David Chelimsky
2011-Jul-29 19:58 UTC
[rspec-users] How can I make before/after all a method?
On Jul 29, 2011, at 12:36 PM, John Hinnegan wrote:> I would like to turn this: > > describe TestClass do > before :all do > # set some config > end > after :all do > # restore some config > end > # do a bunch of tests to this > end > > into > > describe TestClass do > with_config_value(X) > > # do a bunch of tests to this > end > > > Basically, I want to include before :all and after :all clauses into a test. However, I do not want this to apply toa ll tests. > > Thanks in advance.There a number of ways to do this, but if you really just want a method, then just write one! def with_config_value(foo) before(:all) do # stuff that can access foo end after(:all) do # more stuff that can use foo end end HTH, David
On 29 Jul 2011, at 18:36, John Hinnegan wrote:> I would like to turn this: > > describe TestClass do > before :all do > # set some config > end > after :all do > # restore some config > end > # do a bunch of tests to this > end > > into > > describe TestClass do > with_config_value(X) > > # do a bunch of tests to this > end > > > Basically, I want to include before :all and after :all clauses into a test. However, I do not want this to apply toa ll tests. > > Thanks in advance.What you''re talking about is generally called a macro. describe TestClass do def self.with_config_value(config) before :all do #set some config end #etc. end with_config_value(x) do # do a bunch of tests end end You can put the macro in a module and either use #extend to make it available in a describe block, or use RSpec''s config.extend method to do that automatically. Does that help? cheers, Matt -- Freelance programmer & coach Author, http://pragprog.com/book/hwcuc/the-cucumber-book (with Aslak Helles?y) Founder, http://relishapp.com +44(0)7974430184 | http://twitter.com/mattwynne