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.

On 29 Jul 2011, at 18:36, John H. wrote:

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, Search (with Aslak
Hellesy)
Founder, http://relishapp.com
+44(0)7974430184 | http://twitter.com/mattwynne

On Jul 29, 2011, at 12:36 PM, John H. wrote:

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