Check out who we are and our recent projects.

Factory Girl callbacks

Posted: November 25th, 2009 | Author: tarsolya | Filed under: News | Tags: , , , , | 1 Comment »

I’ve been mainly using Shoulda for testing with Test::Unit and Factory Girl helps a lot by letting me stub out simple or more complex object instance variations easily. Now is getting even more easier: callbacks are here!

The three main factory methods all get their respective callbacks, namely: after_build, after_create and after_stub. We can do the following now:

  class Foo < ActiveRecord::Base
    has_many :bar
  end
 
  class Bar < ActiveRecord::Base  
    belongs_to :foo
  end
 
  Factory.define(:foo) do |foo|
    foo.title { "The quick brown fox." }
  end
 
  Factory.define(:bar) do |bar|
    bar.title { "Jumps over the lazy dog." }
  end
 
  Factory.define(:foo_with_bar, :parent => :foo) do |foo|
    foo.after_create { |o| Factory(:bar, :foo => o) }
  end

Neat stuff. 1.2.3 is on Gemcutter now and you can check out more details over at Smashing Robots.


One Comment on “Factory Girl callbacks”

  1. 1 trerweags said at 4:28 pm on June 14th, 2010:

    Hi dude! so enjoyed your site and have a warm feeling inside because of your mission… xpnindustries.com Really to find the in Google. I can give the additional information. viagra shop – yqut – 28522 28634 – by ,
    interesting: Now, two papers in this week’’s Nature make the link between plaque formation and brain dysfunction, providing the first real hope that it may be possible to ameliorate dementia simply by disrupting the deposition of amyloid.Passive avoidance behaviour was studied in a one trial learning, step–through type, passive avoidance task utilizing the natural preference of rats for a dark environment.After either oral or intravenous administration, sildenafil is excreted as metabolites predominantly in the faeces (approximately 80% of administered oral dose) and to a lesser extent in the urine (approximately 13% of administered oral dose).We used standard costing of antidepressants, sildenafil, and unit costs for physician visits within a managed care environment and cost-of-illness methodology to calculate the annualized cost of depression for different treatment outcomes.As a result, U.S. price/earnings ratios are overstated. Good luck!


Leave a Reply