Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

There was a problem

You must be a member of this account.

This project is archived and is in readonly mode.

rake test runs initializers in development environment

#2819

To reproduce:

  1. create a fresh Rails app (rails temp)
  2. run rake db:migrate to generate an empty db/schema.rb
  3. add raise "in development mode!" to the bottom of config/environments/development.rb
  4. run rake test (or just rake); the error is raised:

    Invoke test (first_time) Execute test Invoke test:units (first_time) Invoke db:test:prepare (first_time) Invoke db:abort_if_pending_migrations (first_time) Invoke environment (first_time) Execute environment Invoke test:functionals (first_time) Invoke db:test:prepare Execute test:functionals /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby -I"lib:test" "/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb"
    Invoke test:integration (first_time) Invoke db:test:prepare ** Execute test:integration /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby -I"lib:test" "/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb"
    Errors running test:units!

Alternatively, if you just run rake test:units:

** Invoke test:units (first_time)
** Invoke db:test:prepare (first_time)
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment
rake aborted!
in development mode!
/private/tmp/test/config/environments/development.rb:19:in `load_environment'

My environment:

ruby --version
ruby 1.8.6 (2008-08-11 patchlevel 287) [universal-darwin9.0]

gem list rails
rails (2.3.2, 1.2.6)

Reported by Gaius Centus Novus · June 19th, 2009 @ 06:41 PM

State: stale
Milestone: 3.x
Assigned to: nobody
Importance: none

Activity

  1. Carsten Gehling
    Carsten Gehling

    Confirmed on rails 2.2.2

    June 20th, 2009 @ 01:29 PM

  2. Andy Atkinson
    Andy Atkinson

    Would you mind describing how this is a problem in your application? If I saw this personally, I wouldn't think it is a Rails bug necessarily, perhaps I'm missing something about initializers.

    June 22nd, 2009 @ 07:18 AM

  3. Carsten Gehling
    Carsten Gehling

    It is a problem, if you try to test a Mailer. Since config/environments/development.rb is run instead of config/environments/test.rb, you do not get config.action_mailer.delivery_method = :test set

    This again means, that when your test-suite sends a mail, ActionMailer::Base.deliveries is not updated, so your tests will fail.

    June 22nd, 2009 @ 09:56 AM

  4. Gaius Centus Novus
    Gaius Centus Novus

    I ran into it when I had done a config.middleware.use in my config/development.rb that I expected not to be run in test mode.

    June 22nd, 2009 @ 12:33 PM

  5. Mat Schaffer
    Mat Schaffer

    I ran into this same problem trying to set up Workling in an initializer, but I couldn't select the appropriate dispatcher for testing.

    As a work around I added this to my rspec.rake. I expect you could use a similar strategy for test:unit. I'll probably have to figure that out shortly too.

    spec_prereq = [:set_rails_env, File.exist?(File.join(RAILS_ROOT, 'config', 'database.yml')) ? "db:test:prepare" : :noop]
    
    task :set_rails_env do
      ENV['RAILS_ENV'] = 'test'
    end
    

    This doesn't suppress the evaluation of development.rb, sadly. But at least I can put some if statements in my initializer to do the right thing.

    October 31st, 2009 @ 01:05 AM

  6. Kamal Fariz
    Kamal Fariz
    • Tag changed from 2.3.2, development.rb, evironment, test, testunit to 2.3.2, 2.3.4, development.rb, evironment, test, testunit

    Confirmed on rails 2.3.4

    November 11th, 2009 @ 03:06 PM

  7. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  8. Rohit Arondekar
    Rohit Arondekar
    • State changed from new to stale
    • Importance changed from to

    Marking ticket as stale. If this is still an issue please leave a comment with suggested changes, creating a patch with tests, rebasing an existing patch or just confirming the issue on a latest release or master/branches.

    October 9th, 2010 @ 04:03 AM

  9. bingbing