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

Leave a comment