Factory Girl callbacks
Posted: November 25th, 2009 | Author: tarsolya | Filed under: News | Tags: callbacks, factories, shoulda, tdd, testing | 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.