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?
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) ?
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!
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 a new Rails app.
$ rails test_plugin_env
$ cd test_plugin_env
Create a new plugin.
$ script/generate plugin Foobar
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:
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.
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.
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)
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
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.