Friday, May 25, 2007

ActiveRecord testing ouside of Rails

There is many tips about how to use ActiveRecord outside of Rails but rarely about ActiveRecord testing ouside of Rails. I am working on project that use ActiveRecord ouside of Rails and need to create fixture for testing. It take me a little bit of time to figure out how to do. The source code below demonstrates the trick

require 'test/unit'
require "active_record"
require "active_record/fixtures"

 :adapter => "mysql",
 :host => "localhost",
 :username => "nightlybatch",
 :password => "secret",
 :database => "web_orders"

#this is because fixture verify existence of rails database configuration
ActiveRecord::Base.configurations[:test] = ActiveRecord::Base.connection
class Product < ActiveRecord::Base

class TestFoo < Test::Unit::TestCase
 #set fixture path explicitly
 fixtures :products

 def test_ruby_on_rails_should_exist
  assert products(:ruby_on_rails)


In order to pass the test, the fixture file products.yml shall be created in directory c:/temp, and products table shall be created in MySQL database
   id:   1
   name: ruby on rails
   id:   2
   name: rails recipes

1 comment:

mary Brown said...

Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training in India . Nowadays Java has tons of job opportunities on various vertical industry.