Rails plugins and libraries / Testing plugins
From WhyNotWiki
[edit] test_helper files / Test initialization
[edit]
Example from acts_as_ordered_tree
Notable features:
- Depends on parent app's Rails "environment" (boot.rb) but provides own database and schema. (Bad: Means it can only be tested when installed as plugin within context of an app.)
- Uses sqlite3 (in memory), so:
- it doesn't affect the parent app's test database (so that doesn't have to be reloaded when the plugin test is finished),
- the user of the plugin doesn't need to do any extra configuration to run tests,
- and it doesn't even litter the filesystem with any database files!
./vendor/plugins/acts_as_ordered_tree/test/abstract_unit.rb
ENV["RAILS_ENV"] = "test" require File.dirname(__FILE__) + '/../../../../config/boot' require 'rubygems' require 'test/unit' # The only part of Rails that is actually needed, apparently require 'active_record' # Use a special database for this plugin tests. Output to a local log file. config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml')) ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + '/debug.log') ActiveRecord::Base.establish_connection(config['database']) # Load the plugin itself require File.dirname(__FILE__) + '/../init' # An ActiveRecord model require File.dirname(__FILE__) + '/../lib/person' # Basically equivalent to a migration. Creates the schema/tables. load(File.dirname(__FILE__) + '/schema.rb')
vendor/plugins/acts_as_ordered_tree/test/database.yml
database: adapter: sqlite3 database: ":memory:"
vendor/plugins/acts_as_ordered_tree/test/schema.rb
ActiveRecord::Schema.define(:version => 1) do
create_table "people", :force => true do |t|
t.column "parent_id" ,:integer ,:null => false ,:default => 0
t.column "position" ,:integer
t.column "name" ,:string
end
add_index "people", ["parent_id"], :name => "index_people_on_parent_id"
end
vendor/plugins/acts_as_ordered_tree/lib/person.rb
class Person < ActiveRecord::Base acts_as_ordered_tree end
