Global setup method goes where?

You know how sometimes you look in /log/test.log for debug information
like which test caused a certain render or query?

I found this post which shows how to get test information printed out in
your test.log with test:unit and shoulda, but I’m not sure there’s a way
to do it with RSpec.

http://bmorearty.wordpress.com/2008/06/18/find-tests-more-easily-in-your-testlog/

test:unit allows you to put “setup :blah” in your test_helper.rb, and
then the blah method gets called before each test. This can be used to
print out a log statement saying 'we’re runnig test ___". How would we
do the same in rspec with the spec_helper.rb so that we can print this
data out in our logs too?

Any takers? I’m sure I could probably figure this out if I dove deep
into the code base, but I get the feeling someone must have done this
before.

I’m going to cover this on the next Rails Envy podcast, so you’ll get
some credit for a good answer, and a link if you post the answer to your
blog (assuming you want the publicity). :wink:

Gregg P.
RailsEnvy.com

Gregg P. wrote:

then the blah method gets called before each test. This can be used to
blog (assuming you want the publicity). :wink:

Gregg P.
RailsEnvy.com

In your spec_helper.rb just put:

Spec::Runner.configure do |config|

config.before(:each) do
full_example_description = “#{self.class.description}
#{@method_name}”
RAILS_DEFAULT_LOGGER.info(“\n\nStarting
#{full_example_description}\n\n#{‘-’ * (9 +
full_example_description.length)}”)
end

end

Note that the constant RAILS_DEFAULT_LOGGER is being used since
Rails::logger is only available in 2.1.

I’ll write up a quick post on on this and show how to do something
similar with the story runner.

-Ben

On Fri, Jul 4, 2008 at 4:19 PM, Ben M. [email protected] wrote:

Gregg P. wrote:

I found this post which shows how to get test information printed out in
your test.log with test:unit and shoulda, but I’m not sure there’s a way
to do it with RSpec.

Damn, that is a good idea.

end
Beaten at the gate by about 30 seconds :slight_smile: My implementation was
basically the same!

Note that the constant RAILS_DEFAULT_LOGGER is being used since
Rails::logger is only available in 2.1.

Didn’t think of that bit though. Well done.

Ben M. wrote:

I’m going to cover this on the next Rails Envy podcast, so you’ll get

http://rubyforge.org/mailman/listinfo/rspec-users
Here is the blog post:

http://www.benmabey.com/2008/07/04/global-setup-in-rspec-or-how-to-add-logging-for-specs/

Thanks,
Ben