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 via http://www.ruby-forum.com/.
Scott Taylor
2008-Oct-08 22:35 UTC
[rspec-users] When is spec_helper.rb actually executed?
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 { .. }> Thanks, > Wes > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
Scott Taylor wrote:> > 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 { .. }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
Wes Gamble <weyus at att.net> writes:> Scott Taylor wrote: >> >> 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 { .. } > 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).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
David Chelimsky
2008-Oct-08 23:31 UTC
[rspec-users] When is spec_helper.rb actually executed?
On Wed, Oct 8, 2008 at 5:58 PM, Pat Maddox <pergesu at gmail.com> wrote:> Wes Gamble <weyus at att.net> writes: > >> Scott Taylor wrote: >>> >>> 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?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>>>> >>> >>> 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). > > 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 > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
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 via http://www.ruby-forum.com/.
David Chelimsky
2008-Oct-08 23:46 UTC
[rspec-users] When is spec_helper.rb actually executed?
On Wed, Oct 8, 2008 at 6:40 PM, Wes Gamble <lists at 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.> > Wes
Scott Taylor
2008-Oct-09 01:52 UTC
[rspec-users] When is spec_helper.rb actually executed?
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"> > > 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?Yes. Assuming you have transaction_fixtures = true. This is how rails resets the database between each test (at least after rails 1.2). Scott
Greg Hauptmann
2008-Nov-06 10:33 UTC
[rspec-users] When is spec_helper.rb actually executed?
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 <mailing_lists at railsnewbie.com> wrote:> > 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" > >> >> >> 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? > > Yes. Assuming you have transaction_fixtures = true. This is how rails > resets the database between each test (at least after rails 1.2). > > Scott > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >