Testing plugins : loading fixtures and environment
require 'test/unit'
require 'active_record'
require 'active_record/fixtures'
RAILS_ENV = "test"
require File.expand_path(File.join(File.dirname(__FILE__), '../../../../config/environment.rb'))
class TypoPermalinkWithIdTest < Test::Unit::TestCase
def setup
fixtures_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../../../test/fixtures'))
Fixtures.create_fixtures(fixtures_dir, File.basename("blogs.yml", '.*'))
Fixtures.create_fixtures(fixtures_dir, File.basename("users.yml", '.*'))
end
def test_fixtures_loaded
assert_not_nil Blog.find_by_id(1), "blogs.yml not loaded"
assert_not_nil User.find_by_id(1), "users.yml not loaded"
end
Edit: This idea slightly cribbed from Loading Fixtures in a Migration
Mocked out ActiveRecord for testing plugin
class Resource < ActiveRecord::Base
@@count = 1
def self.count() @@count end
def self.columns() @columns ||= []; end
def self.column(name, sql_type = nil, default = nil, null = true)
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
end
def self.find(param)
return param == :all ? [Resource.new{ |m| m.id = 1; m.name = 'bob' }] : Resource.new{ |m| m.id = 1; m.name = 'bob' }
end
def save() @@count += 1 and return true if valid? end
def destroy() @@count -= 1 end
column :name, :string
end
Manually setting id in Rails 2
In testing DynamicScaffoldResource, I have been working on sort of mocking/overloading ActiveRecord so that it doesn’t touch the DB, so I can minimize how much it depends on the root Rails environment to test, since it’s a plug-in.
class Resource < ActiveRecord::Base
def self.columns() @columns ||= []; end
def self.column(name, sql_type = nil, default = nil, null = true)
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
end
def self.find(param)
return param == :all ? [Resource.new(:id => 1, :name => 'bob')] : Resource.new(:id: => 1, :name => 'bob')
end
column :id, :integer
column :name, :string
validates_presence_of :name
end
Well Rails treats the id as a special field, since it is. I tried letting it implicitely add it, and explicitely as above, but there was no way I seemed to be able to set it manually and access it (maybe the problem is prior to a save?), so my edit named route in the view wouldn’t choke (the show path had no problem with the nil, which I might not have caught for a while). While there is probably some way to open up access to the id column, this mailing list post opened up an alternative that works, just use:
Resource.new{ |m| m.id = 1; m.name = 'bob' }
curl-ifying Firefox
I finally found an extension that lets me use Firefox for testing that I would previously had to use curl for, namely submitting text/xml only requests (for testing REST APIs, for example) or similar. I thought ModifyHeaders would do the trick, but I never really got that working. But TamperData is exactly what I wanted.
Trac on Dreamhost
I just got Trac running on Dreamhost for InfoSauce (basically my) Open Source Projects to make it the home of the new Dynamic Scaffold Resource plugin for Rails
The Dreamhost Wiki entry on Trac made it a breeze, if only because of the link to an automated install script .
My only gripe would be that their section on Pretty URLs had a minor bug (for me).DirectoryIndex wiki
should be
DirectoryIndex index.fcgi

Articles via rss or email