Full plugin test environment (long)

Testing part of rails framework works very nice. Unfortunately testing
plugins seems to be a pain (at least for me).

I’m writing a simple plugin that adds a model and a controller mixin (it
seems to work fine in my application). Now I’d like to write tests for
it. They’ll require at least a controller (that can be mixed into) and
database (probably no fixtures).

I’d also like the test to be independent of any applications, so I
thought that using in-memory sqlite db would be quite nice.

Based on tests of plugin_dependencies and plugin_migrations plugins I’ve
managed to create something like this in my plugin’s test subdirectory:

rails_root/
rails_root/config/
rails_root/config/boot.rb
rails_root/config/database.yml
rails_root/config/environment.rb
rails_root/config/environments/in_memory.rb
unit/
unit/stored_action_test.rb
test_helper.rb

The file boot.rb has been copied from main rails-app config directory.
environment.rb just requires boot.rb. in_memory.rb file is empty.

database.yml defines an sqlite :memory: adapter.

test_helper.rb seems to be the most important file here, this is its
contents:

require ‘rubygems’

ENV[‘RAILS_ENV’] ||= ‘in_memory’

Load test unit

require ‘test/unit’

Load rails (TODO: there must be a better way)

require ‘active_record’
require ‘active_record/base’
require ‘active_record/fixtures’
require ‘action_controller’

Load the plugin

#$LOAD_PATH << File.join(File.dirname(FILE), ‘…’, ‘lib’)
require File.join(File.dirname(FILE), ‘…’, ‘init’)

There is also one unit test (stored_action_test.rb):

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

class StoredActionTest < Test::Unit::TestCase

def test_key
puts “Test 1”
stored_action = StoredAction.new(:action => ‘test’)
puts “Test 2”
assert(stored_action.save)
key = stored_action.key
end
end

Running “rake test” from plugin’s main directory gives me this:

cinek [14:47] vendor/plugins/action_store > rake test
(in /home/cinek/src/gallery/vendor/plugins/action_store)
/usr/local/bin/ruby18 -Ilib:lib
“/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.2/lib/rake/rake_test_loader.rb”
“test/functional/action_store_test.rb” “test/unit/stored_action_test.rb”
Loaded suite
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.2/lib/rake/rake_test_loader
Started
FTest 1

The test clearly fails but it gives me no idea why creation of
StoredAction doesn’t succeed (it is the model that the plugin supplies.
It works fine within the application).

So I have a question: what is the proper way of creating test
environment to:

  • get some more information in cases like this,
  • not have to require every bit of rails that is used by tests by hand
    in test_helper.rb,
  • create schema.rb for the tests (I haven’t tried doing this yet),
  • that would not require in-depth knowledge of rails implementation (I
    don’t believe in that one :).

I know that there may be some issues with in-memory sqlite3 database,
but I guess I haven’t reached that point yet.

Sorry for the long mail, but since I’ve got this far (and it took me a
lot of time :slight_smile: I thought that it might be easier for you to help me
carry on from this point than from the beginning.

I have found a quite a few blog entries on the net that scratch the
surface, but it’s difficult for me to put these tiny pieces together
into something meaningful :-/

Thanks in advance!

Marcin S.

Marcin,

Is it worth a hassle? We write lots of plugin all covered with tests.
We don’t include tests with plugins though but each plugin development
tree has its own rails application just for testing the plugin. For
example, to install our ActsAsSecure plugin, you run ‘script/plugin
install
svn://rubyforge.org/var/svn/acts-as-secure/trunk/vendor/plugins/acts_as_secure’.
As you can see it is located under vendor/plugin while the tests are
under svn://rubyforge.org/var/svn/acts-as-secure/trunk/test. It does
not give others an ability to run tests for your plugin (unless they
check out the whole app just for that) but it makes it much easier for
you to test it during development.

Regards,
Val

http://revolutiononrails.blogspot.com/

On Apr 29, 8:57 am, Marcin S. <rails-mailing-l…@andreas-

Val wrote:

Is it worth a hassle? We write lots of plugin all covered with tests.
We don’t include tests with plugins though but each plugin development
tree has its own rails application just for testing the plugin. For

Yes, this should be a lot easier. The funny thing is that I remember
thinking about this approach :slight_smile:
For the record: my test above fails for some unknown reason, even when
tested in its own application.

Anyway, I found this, which is the answer to my question:
http://www.pluginaweek.org/2006/11/24/plugin-tip-of-the-week-testing-your-plugins-the-right-way/

Marcin S.