Plugins and the test environment

Hi guys. I’m writing a plugin for use in the test environment.
However, when I run rake test'' orrake spec’’, RAILS_ENV is set
to “development” when my plugin’s init.rb is run.

How do you configure a plugin to load require a file in the test
environment?

Thanks,
Nick

On 8 Oct 2008, at 23:17, Nick wrote:

Hi guys. I’m writing a plugin for use in the test environment.
However, when I run rake test'' orrake spec’’, RAILS_ENV is set
to “development” when my plugin’s init.rb is run.

How do you configure a plugin to load require a file in the test
environment?

Are you getting hoodwinked by the fact that the development
environment is loaded once in order to dump its database (and then the
test environment loads) ?

Fred

On Oct 9, 4:10 am, Frederick C. [email protected]
wrote:

Are you getting hoodwinked by the fact that the development environmentis loaded once in order to dump its database (and then the testenvironmentloads) ?

Fred

I think something strange was going on, as the test environment wasn’t
running at all. I’ve sorted it out now though. Thanks, mate!

On Oct 9, 4:10 am, Frederick C. [email protected]
wrote:

Are you getting hoodwinked by the fact that the development environmentis loaded once in order to dump its database (and then the testenvironmentloads) ?

Fred

Hi Fred. I seem to have run into this problem again. I’ll outline my
exact steps for you:

  1. Create a new Rails app.
    $ rails test_plugin_env
    $ cd test_plugin_env

  2. Create a new plugin.
    $ script/generate plugin Foobar

  3. Add the following to vendor/plugins/foobar/init.rb
    if defined? RAILS_ENV
    puts “foobar > init.rb > RAILS_ENV = [#{RAILS_ENV}]”
    puts “foobar > init.rb > test environment!” if RAILS_ENV == ‘test’
    end

If you then run whatever tests exists in an empty Rails app, you’ll
see that RAILS_ENV is never set to “test”. For example:

$ rake test
(in /Users/nick/src/test_plugin_env)
foobar > init.rb > RAILS_ENV = [development]
/opt/local/bin/ruby -Ilib:test “/opt/local/lib/ruby/gems/1.8/gems/
rake-0.8.1/lib/rake/rake_test_loader.rb”
/opt/local/bin/ruby -Ilib:test “/opt/local/lib/ruby/gems/1.8/gems/
rake-0.8.1/lib/rake/rake_test_loader.rb”
/opt/local/bin/ruby -Ilib:test “/opt/local/lib/ruby/gems/1.8/gems/
rake-0.8.1/lib/rake/rake_test_loader.rb”
$

Why is RAILS_ENV never set to “test”?

Thanks again, Fred!
-Nick

On 14 Oct 2008, at 19:03, Nick wrote:

exact steps for you:
Create at least one test in that app and it should be fine.

Fred

On Oct 14, 2:14 pm, Frederick C. [email protected]
wrote:

Create at least one test in that app and it should be fine.

Fred

I created a basic unit test:

$ cat test/unit/foobar_test.rb
require File.dirname(FILE) + ‘/…/test_helper’

class FoobarTest < Test::Unit::TestCase
def test_true
assert true, true
end
end

But when I ran the test, the development environment was loaded before
the test environment:

—START OUTPUT—
$ rake test:units
(in /Users/nick/src/test_plugin_env)
foobar > init.rb > RAILS_ENV = [development]
/opt/local/bin/ruby -Ilib:test “/opt/local/lib/ruby/gems/1.8/gems/
rake-0.8.1/lib/rake/rake_test_loader.rb” “test/unit/foobar_test.rb”
foobar > init.rb > RAILS_ENV = [test]
foobar > init.rb > test environment!
Loaded suite /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake/
rake_test_loader
Started
.
Finished in 0.216451 seconds.

1 tests, 1 assertions, 0 failures, 0 errors
—END OUTPUT—

Why is the development environment loaded before the test environment?

Thanks,
Nick

On Oct 14, 7:22 pm, Nick [email protected] wrote:

On Oct 14, 2:14 pm, Frederick C. [email protected]
wrote:

Why is the development environment loaded before the test environment?

Because the first thing that rake test does is clone the development
database. Were you to run just one test (ie ruby test/unit/
foo_test.rb) you wouldn’t see the dev environment loaded.

Fred

On Oct 14, 2:34 pm, Frederick C. [email protected]
wrote:

On Oct 14, 7:22 pm, Nick [email protected] wrote:> On Oct 14, 2:14 pm, Frederick C. [email protected]

wrote:

Why is the development environment loaded before the test environment?

Because the first thing that rake test does is clone the development
database. Were you to run just one test (ie ruby test/unit/
foo_test.rb) you wouldn’t see the dev environment loaded.

Fred

I see. That explains things a bit now.

With that in mind, how would you configure a plugin to be loaded only
in the development environment, and not in the test environment? Since
running a suite of tests first jumps into the development environment,
I’m not sure how to go about this.

Cheers,
Nick

On Oct 14, 7:49 pm, Nick [email protected] wrote:

foo_test.rb) you wouldn’t see the dev environment loaded.

Fred

I see. That explains things a bit now.

With that in mind, how would you configure a plugin to be loaded only
in the development environment, and not in the test environment? Since
running a suite of tests first jumps into the development environment,
I’m not sure how to go about this.

you could certainly bracket the body of your init.rb with “if
RAILS_ENV ==” and do nothing if not (except perhaps remove your
plugin’s lib directory from the load path)

Fred

On Oct 14, 7:49 pm, Nick [email protected] wrote:

With that in mind, how would you configure a plugin to be loaded only
in the development environment, and not in the test environment? Since
running a suite of tests first jumps into the development environment,
I’m not sure how to go about this.

On Oct 14, 3:32 pm, Frederick C. [email protected]
wrote:

you could certainly bracket the body of your init.rb with “if
RAILS_ENV ==” and do nothing if not (except perhaps remove your
plugin’s lib directory from the load path)

Fred

Hi Fred. I’m afraid I don’t follow. If I put this in a plugin’s
init.rb :
require ‘foobar’ if RAILS_ENV == ‘development’
Then ‘foobar’ will be loaded whenever any test suite, such as rake test'' or rake spec’', is run, because rake initially loads the
development environment.
-Nick