Forum: Ruby on Rails Plugins and the test environment

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
49de4cd2f26705785cbef2b15a9df7aa?d=identicon&s=25 Nick (Guest)
on 2008-10-09 00:18
(Received via mailing list)
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?

Thanks,
Nick
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-10-09 10:11
(Received via mailing list)
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'' 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) ?

Fred
49de4cd2f26705785cbef2b15a9df7aa?d=identicon&s=25 Nick (Guest)
on 2008-10-10 02:41
(Received via mailing list)
On Oct 9, 4:10 am, Frederick Cheung <frederick.che...@gmail.com>
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!
49de4cd2f26705785cbef2b15a9df7aa?d=identicon&s=25 Nick (Guest)
on 2008-10-14 20:04
(Received via mailing list)
On Oct 9, 4:10 am, Frederick Cheung <frederick.che...@gmail.com>
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
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-10-14 20:15
(Received via mailing list)
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
49de4cd2f26705785cbef2b15a9df7aa?d=identicon&s=25 Nick (Guest)
on 2008-10-14 20:22
(Received via mailing list)
On Oct 14, 2:14 pm, Frederick Cheung <frederick.che...@gmail.com>
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
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-10-14 20:35
(Received via mailing list)
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
49de4cd2f26705785cbef2b15a9df7aa?d=identicon&s=25 Nick (Guest)
on 2008-10-14 20:50
(Received via mailing list)
On Oct 14, 2:34 pm, Frederick Cheung <frederick.che...@gmail.com>
wrote:
> 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.

Cheers,
Nick
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-10-14 21:33
(Received via mailing list)
On Oct 14, 7:49 pm, Nick <n...@deadorange.com> 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
49de4cd2f26705785cbef2b15a9df7aa?d=identicon&s=25 Nick (Guest)
on 2008-10-14 23:39
(Received via mailing list)
> On Oct 14, 7:49 pm, Nick <n...@deadorange.com> 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 Cheung <frederick.che...@gmail.com>
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
This topic is locked and can not be replied to.