public
Description: A less sucky way to do fixtures, sort of
Clone URL: git://github.com/pat-maddox/test_exemplars.git
pat (author)
Wed Aug 08 19:41:35 -0700 2007
commit  388e8e5a707531c6ad922c59990c498bb776920d
tree    3176216dc56aeb23c1a262d8ab3a399919f4810e
parent  67f463d4f64c8bdaa8c4e9b5b729c3f893ca3899
name age message
file MIT-LICENSE Wed Aug 08 03:30:02 -0700 2007 initial import [pat]
file README Wed Aug 08 12:02:21 -0700 2007 auto id field [pat]
file init.rb Wed Aug 08 03:30:02 -0700 2007 initial import [pat]
file install.rb Wed Aug 08 03:30:02 -0700 2007 initial import [pat]
directory lib/ Wed Aug 08 19:41:35 -0700 2007 fixed funky class var name clashing bug [pat]
directory spec/ Wed Aug 08 19:41:35 -0700 2007 fixed funky class var name clashing bug [pat]
directory tasks/ Wed Aug 08 03:30:02 -0700 2007 initial import [pat]
file uninstall.rb Wed Aug 08 03:30:02 -0700 2007 initial import [pat]
README
TestExemplars
=============

This plugin is based off of Piers Cawley's post at http://www.bofh.org.uk/articles/2007/08/05/doing-the-fixture-thing.  
It allows you to easily create valid ActiveRecord objects for use in testing.

Usage is really simple.  In your test_helper.rb or spec_helper.rb file just
require "test_exemplars"
include ExemplarBuilder

This provides you with the exemplify method that you can use to create an exemplar for any AR class.

Your best bet is to take a look at spec/test_exemplars_spec.rb to grok the usage.  Here are a couple examples though.  
Assume the following ActiveRecord class:

  class Chicken < ActiveRecord::Base
    attr_protected :ssn
  end

  # Basic exemplar with attributes
  exemplify Chicken, :name => "chicken little", :age => 10
  Chicken.exemplar    => #<Chicken:0x31227f0 @attributes={"name"=>"chicken little", "ssn"=>nil, "age"=>10}, 
  @new_record=true>
  
  # Override attributes
  exemplify Chicken, :name => "chicken little", :age => 10
  Chicken.exemplar(:name => "amazing")    => #<Chicken:0x3120374 @attributes={"name"=>"amazing", "ssn"=>nil, "age"=>10}, 
  @new_record=true>
  
exemplify also takes an optional block.  This is useful for methods you need to call on the exemplar.  For example, 
attr_protected attributes can't be mass-assigned, you need to explicitly set them.  Of course you can use it for any 
object initialization that isn't basic hash attributes.

  # ssn is ignored because of attr_protected
  exemplify Chicken, :ssn => "abc123"
  Chicken.exemplar    => #<Chicken:0x31377b8 @attributes={"name"=>nil, "ssn"=>nil, "age"=>nil}, @new_record=true>
  
  # ssn is assigned in a block
  exemplify(Chicken) {|c| c.ssn = "abc123" }
  Chicken.exemplar    => #<Chicken:0x3136e1c @attributes={"name"=>nil, "ssn"=>"abc123", "age"=>nil}, @new_record=true>
  
Finally, you can automatically populate a field's value with an autoincrementing exemplar ID.

  # auto populate name field
  exemplify Chicken, :auto_id => :name
  Chicken.exemplar    => #<Chicken:0x311d638 @new_record=true, @attributes={"name"=>"Chicken1", "ssn"=>nil, "age"=>nil}>

    Chicken.exemplar    => #<Chicken:0x311d638 @new_record=true, @attributes={"name"=>"Chicken2", "ssn"=>nil, "age"=>nil}>

    
You can also automatically save the record with ARClass.create_exemplar and ARClass.create_exemplar!.  They're just 
convenience methods that build an exemplar and call #save and #save! respectively.