Forum: RSpec When is spec_helper.rb actually executed?

Posted by Wes Gamble (weyus)
on 2008-10-09 00:26
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
Posted by Scott Taylor (Guest)
on 2008-10-09 00:35
(Received via mailing list)
On Oct 8, 2008, at 6:26 PM, Wes Gamble 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 { .. }
Posted by Wes Gamble (weyus)
on 2008-10-09 00:50
(Received via mailing list)
Scott Taylor 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
Posted by Pat Maddox (pergesu)
on 2008-10-09 00:59
(Received via mailing list)
Wes Gamble <weyus@att.net> 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
Posted by David Chelimsky (Guest)
on 2008-10-09 01:31
(Received via mailing list)
On Wed, Oct 8, 2008 at 5:58 PM, Pat Maddox <pergesu@gmail.com> 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
Posted by Wes Gamble (weyus)
on 2008-10-09 01:40
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
Posted by David Chelimsky (Guest)
on 2008-10-09 01:46
(Received via mailing list)
On Wed, Oct 8, 2008 at 6:40 PM, Wes Gamble <lists@ruby-forum.com> 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.
Posted by Scott Taylor (Guest)
on 2008-10-09 03:53
(Received via mailing list)
On Oct 8, 2008, at 7:40 PM, Wes Gamble 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
Posted by Greg Hauptmann (Guest)
on 2008-11-06 11:38
(Received via mailing list)
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 Taylor
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.