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
ENV[‘RAILS_ENV’] ||= ‘in_memory’
Load test unit
Load rails (TODO: there must be a better way)
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
puts “Test 1”
stored_action = StoredAction.new(: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
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
- get some more information in cases like this,
- not have to require every bit of rails that is used by tests by hand
- 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!