# When is spec\_helper.rb actually executed?

**URL:** <https://www.ruby-forum.com/t/when-is-spec-helper-rb-actually-executed/150477>\
**Category:** RSpec\
**Created:** [October 9, 2008, 12:26am UTC](https://www.ruby-forum.com/t/when-is-spec-helper-rb-actually-executed/150477 "2008-10-09T00:26:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [October 9, 2008, 12:26am UTC](https://www.ruby-forum.com/t/when-is-spec-helper-rb-actually-executed/150477/1 "2008-10-09T00:26:11Z")

</div>

I’m trying to debug what I suspect is a case where spec\_helper.rb is not  
being executed.

I put a puts statement inside the Spec::Runner.configure do |config|  
block, and I can’t ever see the printout.

When does spec\_helper.rb get run during rspec operation?

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [October 9, 2008, 12:35am UTC](https://www.ruby-forum.com/t/when-is-spec-helper-rb-actually-executed/150477/2 "2008-10-09T00:35:17Z")

</div>

On Oct 8, 2008, at 6:26 PM, Wes G. wrote:

> I’m trying to debug what I suspect is a case where spec\_helper.rb is  
> not  
> being executed.
> 
> I put a puts statement inside the Spec::Runner.configure do |config|  
> block, and I can’t ever see the printout.
> 
> When does spec\_helper.rb get run during rspec operation?

What does your spec\_helper.rb contain?

AFAIK, it gets executed before anything else - usually it contains  
Spec::Runner.configure { … }

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [October 9, 2008, 12:59am UTC](https://www.ruby-forum.com/t/when-is-spec-helper-rb-actually-executed/150477/3 "2008-10-09T00:59:16Z")

</div>

Wes G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> > > When does spec\_helper.rb get run during rspec operation?
> 
> to the “spec” task and now it gets all of the config from spec\_helper  
> (including RAILS\_ENV which was not being set before).

You don’t want to put this in the spec task itself…you should require  
it from your spec files. It wasn’t ever being run because it wasn’t  
ever being loaded 🙂

Pat

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [October 9, 2008, 12:50am UTC](https://www.ruby-forum.com/t/when-is-spec-helper-rb-actually-executed/150477/4 "2008-10-09T00:50:30Z")

</div>

Scott T. wrote:

> > 
> 
> What does your spec\_helper.rb contain?
> 
> AFAIK, it gets executed before anything else - usually it contains  
> Spec::Runner.configure { … }  
> It is the standard default spec\_helper.rb (see below). I could find no  
> reference to it in the rake “spec” task. I added this:

require ‘…/…/spec/spec\_helper’

to the “spec” task and now it gets all of the config from spec\_helper  
(including RAILS\_ENV which was not being set before).

Wes

It contains:

# This file is copied to ~/spec when you run 'ruby script/generate

rspec’

# from the project root directory.

ENV[“RAILS\_ENV”] = “test”  
require File.expand\_path(File.dirname( **FILE** ) +  
“/…/config/environment”)  
require ‘spec’  
require ‘spec/rails’

Spec::Runner.configure do |config|  
puts “In config block of Spec::Runner”

# If you’re not using ActiveRecord you should remove these

# lines, delete config/database.yml and disable :active\_record

# in your config/boot.rb

config.use\_transactional\_fixtures = true  
config.use\_instantiated\_fixtures = false  
config.fixture\_path = RAILS\_ROOT + ‘/spec/fixtures/’

# == Fixtures

# 

# You can declare fixtures for each example\_group like this:

# describe “…” do

# fixtures :table\_a, :table\_b

# 

# Alternatively, if you prefer to declare them only once, you can

# do so right here. Just uncomment the next line and replace the

fixture

# names with your fixtures.

# 

# config.global\_fixtures = :table\_a, :table\_b

# 

# If you declare global fixtures, be aware that they will be declared

# for all of your examples, even those that don’t use them.

# 

# == Mock Framework

# 

# RSpec uses it’s own mocking framework by default. If you prefer to

# use mocha, flexmock or RR, uncomment the appropriate line:

# 

# config.mock\_with :mocha

# config.mock\_with :flexmock

# config.mock\_with :rr

end

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [October 9, 2008, 1:40am UTC](https://www.ruby-forum.com/t/when-is-spec-helper-rb-actually-executed/150477/5 "2008-10-09T01:40:51Z")

</div>

OK, got it.

I have a follow-up question though. If a given spec DID NOT require  
spec\_helper.rb, doesn’t that imply that the spec would be run against  
the Rails “development” environment.

I just did a test where I printed out the configuration of one of my  
classes DB connnections (e.g. Blah.connection.inspect where Blah is an  
AR model). And I saw that the “database” setting in this was  
development. My spec definitely inserts data into the DB.

I kind of expected to see additional rows in the table in question.

But I just realized that each spec is probably surrounded by a  
transaction - is that correct?

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [October 9, 2008, 1:31am UTC](https://www.ruby-forum.com/t/when-is-spec-helper-rb-actually-executed/150477/6 "2008-10-09T01:31:26Z")

</div>

On Wed, Oct 8, 2008 at 5:58 PM, Pat M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > > block, and I can’t ever see the printout.
> > > > 
> > > > When does spec\_helper.rb get run during rspec operation?

RSpec does not “run” spec\_helper. By default, rspec loads files that  
end with “\_spec.rb” and it is up to those files to require  
spec\_helper.rb.

HTH,  
David

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [October 9, 2008, 1:46am UTC](https://www.ruby-forum.com/t/when-is-spec-helper-rb-actually-executed/150477/7 "2008-10-09T01:46:39Z")

</div>

On Wed, Oct 8, 2008 at 6:40 PM, Wes G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> OK, got it.
> 
> I have a follow-up question though. If a given spec DID NOT require  
> spec\_helper.rb, doesn’t that imply that the spec would be run against  
> the Rails “development” environment.

Not necessarily. Depends on what was loaded before that file. So if  
first\_spec.rb requires spec\_helper.rb and second\_spec.rb does not, if  
they run in order (first, second) then since the first loads  
spec\_helper.rb the second will run in the “test” environment.

In that same scenario, if you run second\_spec.rb by itself, it will  
run in development.

> I just did a test where I printed out the configuration of one of my  
> classes DB connnections (e.g. Blah.connection.inspect where Blah is an  
> AR model). And I saw that the “database” setting in this was  
> development. My spec definitely inserts data into the DB.
> 
> I kind of expected to see additional rows in the table in question.
> 
> But I just realized that each spec is probably surrounded by a  
> transaction - is that correct?

As long as you see this in spec\_helper.rb (and it is loaded 😉 )

config.use\_transactional\_fixtures = true

That variable has NOTHING to do with fixtures, but it is rails hook  
into running each test (using test/unit) or spec (using rspec) in a  
transaction.

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [November 6, 2008, 11:38am UTC](https://www.ruby-forum.com/t/when-is-spec-helper-rb-actually-executed/150477/8 "2008-11-06T11:38:10Z")

</div>

can someone advise how best to handle the situation where I want to  
keep (i.e. not have deleted) my configuration intact?  
Perhaps seed it in environment/test.rb - but how do I get a normal  
rails line of code to run in the “test.rb” file (i.e. only for when  
starting up in test mode)?  
I get “ActiveRecord::ConnectionNotEstablished” when I include  
“RecurringType.create(:name =\> “TYPE\_BASIC”)”…

On Thu, Oct 9, 2008 at 11:52 AM, Scott T.

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [October 9, 2008, 3:53am UTC](https://www.ruby-forum.com/t/when-is-spec-helper-rb-actually-executed/150477/9 "2008-10-09T03:53:10Z")

</div>

On Oct 8, 2008, at 7:40 PM, Wes G. wrote:

> OK, got it.
> 
> I have a follow-up question though. If a given spec DID NOT require  
> spec\_helper.rb, doesn’t that imply that the spec would be run against  
> the Rails “development” environment.

Basically, the normal operation is that your spec requires  
spec\_helper, which eventually requires the test\_helper which comes  
with rails, which sets the constant:

RAILS\_ENV = “test”

> transaction - is that correct?  
> Yes. Assuming you have transaction\_fixtures = true. This is how  
> rails resets the database between each test (at least after rails 1.2).

Scott
