Testing plugins : loading fixtures and environment

Posted by Tim Connor Sun, 22 Oct 2006 21:44:00 GMT

One of the first things I learned working on the typo-permalink-with-id plugin was how to load fixtures from a plugin. I had previously just been mocking out ActiveRecord, but the Typo object model is complicated enough that it was easier to just schlup their envirnment.rb and then load their fixtures into my tests.

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

Posted by Tim Connor Fri, 20 Oct 2006 06:06:00 GMT

I mentioned this in my previous post, but to make my plugin easier to test I tried to make it not need to touch the DB, which was interesting to do, given that scaffold_resource is very much tied to a model. This was a big inspiration and this the final (as of now) result:

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

Posted by Tim Connor Fri, 20 Oct 2006 00:12:00 GMT

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

Posted by Tim Connor Tue, 17 Oct 2006 23:10:00 GMT

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

Posted by Tim Connor Tue, 17 Oct 2006 19:24:00 GMT

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

Older posts: 1 2 3