Engines 1.2 and Testing

Hi all,

I’m new to this list as well as to engines in general. I am taking my
first
stab at writing an engine and am having a little trouble getting the
tests
for my engine working. Everything else is working at this point, e.g.
controllers, models, migrations, etc. The last step is to get testing
working.

Can I get a general overview on how tests are supposed to work? I’m
using
Rails 1.2.1 and the release branch of Engines 1.2.

Basically, at this point, I copied the tests from the surrounding
application in to my engine’s test folder, so my dir structure looks
like

my_engine

  • app
  • lib
  • test
    • fixtures/
    • functional/
    • integration/
    • mocks/
    • unit/
    • test_helper.rb

Problems I’m having:

1). Fixtures don’t seem to be loading. I read in the docs that I need
to
call Engines::Testing.set_fixture_path first, but a) I couldn’t get it
to
recognize Engines, and b) it looks like the rake plugins:units task does
this for me—I think.

2). I had a file in my engine’s lib folder that I need to include. I
couldn’t get it to include normally. I ended up getting it to work
with:

require File.dirname(FILE) + ‘/…/lib/authenticated_test_helper’
include AuthenticatedTestHelper

where I used to be able to call include AuthenticatedTestHelper by
itself.
Are files in lib not automatically loaded?

3). In my original test_helper.rb there is the TestCase section, where
you
set certain options like self.use_transactional_fixtures, I was unable
to
get this to work. So, right now, I’ve commented them out.

4). What requires should be where? In the original files, there’s

ENV[“RAILS_ENV”] = “test”
require File.expand_path(File.dirname(FILE) +
“/…/config/environment”)
require ‘test_help’

in test_helper.rb - Do these still need to be in my engine’s
test_helper.rb?

I think that’s about it. Through various tracking down errors, I’ve
managed
to get the tests to run, but am still getting errors whenever my test
calls
up a fixture with something like users(:trey). Because I’ve done so
much
random tweaking, I thought I’d ask what the preferred way is.

Trey

Hi Trey,

On 1/27/07, Trey B. [email protected] wrote:

1). Fixtures don’t seem to be loading. I read in the docs that I need to
call Engines::Testing.set_fixture_path first, but a) I couldn’t get it to
recognize Engines, and b) it looks like the rake plugins:units task does
this for me—I think.

The provided rake tasks will call
Engines::Testing.setup_plugin_fixtures, but you need to call
Engines::Testing.set_fixture_path yourself in your tests to redirect
Test::Unit to the new temporary fixture directory.

However, more troubling is the fact that your code doesn’t find the
engines plugin. Are you running the tests with the supplied rake
tasks, or manually (i.e. ruby /path/to/my/test.rb)?

2). I had a file in my engine’s lib folder that I need to include. I
couldn’t get it to include normally. I ended up getting it to work with:

require File.dirname(FILE) +
‘/…/lib/authenticated_test_helper’
include AuthenticatedTestHelper

where I used to be able to call include AuthenticatedTestHelper by itself.
Are files in lib not automatically loaded?

I believe a change in the way that Rails works means that some classes
aren’t automagically loaded when they are referenced. Lib directories
are certainly available in the load path (you can check this yourself
by puts-ing $LOAD_PATH at the top of your tests), so a simple “require
‘file’” should be sufficient. Again - this seems like it might be
related to your not being able to reference the Engines module.

in test_helper.rb - Do these still need to be in my engine’s test_helper.rb?

If your tests require the Rails environment to be loaded, yes.

Actually, this might relate to a possible source of problems - you
won’t be able to work with any of the engines functionality until you
load the rails environment (and so all the plugins) - are you trying
to reference anything within any plugins before these lines?

Does any of the above help?

Okay, I verified that I am running the tests with the engines task
test:plugins:units (I inserted a puts statement in
/vendor/plugins/engines/tasks/engines.rake and it was outputted).

I then have two files in /vendor/plugins/tandem/test/unit/: page_test.rb
and
user_test.rb

Both of these files are structured like:

require File.dirname(FILE) + ‘/…/test_helper’

class UserTest < Test::Unit::TestCase
#some test methods
end

I have verified that /vendor/plugins/tandem/test/test_helper.rb is
getting
called by placing puts $LOAD_PATH at the very top of the file.

It outputs:

lib
/usr/local/lib/ruby/site_ruby/1.8
/usr/local/lib/ruby/site_ruby/1.8/i686-darwin8.5.2
/usr/local/lib/ruby/site_ruby
/usr/local/lib/ruby/1.8
/usr/local/lib/ruby/1.8/i686-darwin8.5.2
.

Below the $LOAD_PATH put, my test_helper.rb has:

#ENV[“RAILS_ENV”] = “test”
#require File.expand_path(File.dirname(FILE) +
“/…/config/environment”)
require ‘test/unit’
require File.dirname(FILE) + ‘/…/lib/authenticated_test_helper’
include AuthenticatedTestHelper

The top two lines are commented out because if they aren’t, then I get
the
error:

./vendor/plugins/tandem/test/unit/…/test_helper.rb:5:in `require’: no
such
file to load –
/Users/treybean/Sites/project/vendor/plugins/tandem/config/environment
(LoadError)
from ./vendor/plugins/tandem/test/unit/…/test_helper.rb:5
from ./vendor/plugins/tandem/test/unit/page_test.rb:1
from /usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1
/lib/rake/rake_test_loader.rb:5
from /usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1
/lib/rake/rake_test_loader.rb:5
rake aborted!
Command failed with status (1): [/usr/local/bin/ruby -Ilib
"/usr/local/lib/…]

And if I put Engines::Testing.set_fixture_path on line 2 (just below the
LOAD_PATH line), I get this error:

./vendor/plugins/tandem/test/unit/…/test_helper.rb:2: uninitialized
constant Engines (NameError)
from ./vendor/plugins/tandem/test/unit/page_test.rb:1
from /usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1
/lib/rake/rake_test_loader.rb:5
from /usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1
/lib/rake/rake_test_loader.rb:5
rake aborted!
Command failed with status (1): [/usr/local/bin/ruby -Ilib
"/usr/local/lib/…]

Man, I feel like I’m so close, but either Rails or Engines (more likely
both) isn’t being initialized prior to the tests. Maybe the line

require File.expand_path(File.dirname(FILE) +
“/…/config/environment”)

needs to be modified to actually point at the config/environment in the
main
application, rather than looking for it in my engine?

Does this shed any light on what might be going on here?

Thanks for you help on this. I am so excited to get this last piece in
place. It looks like you’ve done some great work here.

Trey

Hello again

I’ve just updated the engines plugin source with a few tweaks, mostly
aimed at testing. Update and have a look. In particular, the generated
documentation now includes more information about running tests and
creating a test_helper.

  • James

Hi again Trey -

Yeah, you’ll definitely need to modify the line copied from Rails’
main test_helper so that it does actually point to the main
environment.rb file - I should’ve spotted that earlier! If you have
RAILS_ROOT/vendor/plugins/my_plugin/test/test_helper.rb, it should
contain

require File.expand_path(File.dirname(FILE) +
“/…/…/…/…/config/environment”)

You might need to double-check that, but that should be the key. Once
this file loads, all the plugins (and in particular the engines
testing extensions) will be available.

  • james

Wooo-hooo! That did it. Thanks so much for your help. I’ll let you
know
if I come across anything else.

Thanks again,
Trey