Hi guys. I'm writing a plugin for use in the test environment.
However, when I run ``rake test'' or ``rake 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) ?
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:
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:
Create at least one test in that app and it should be fine.
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.
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, 7:22 pm, Nick <n...@deadorange.com> wrote:> On Oct 14, 2:14 pm, Frederick Cheung <frederick.che...@gmail.com>
> > 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.
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)
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