Forum: Ruby on Rails Full plugin test environment (long)

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
40caa7d562f4c348efe949638918d367?d=identicon&s=25 Marcin Simonides (cinek)
on 2007-04-29 14:57
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:


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

 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 = => 'test')
     puts "Test 2"
     key = stored_action.key

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
"test/functional/action_store_test.rb" "test/unit/stored_action_test.rb"
Loaded suite
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 :) 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 Simonides
971708ecf1c0b3cada6cf98ea58611fd?d=identicon&s=25 Val (Guest)
on 2007-04-29 15:20
(Received via mailing list)

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
As you can see it is located under vendor/plugin while the tests are
under svn:// 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.


On Apr 29, 8:57 am, Marcin Simonides <rails-mailing-l...@andreas-
40caa7d562f4c348efe949638918d367?d=identicon&s=25 Marcin Simonides (cinek)
on 2007-04-29 20:22
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 :)
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:
Marcin Simonides
This topic is locked and can not be replied to.