Skip to content

Instantly share code, notes, and snippets.

@thinkerbot
Created October 1, 2012 16:29
Show Gist options
  • Save thinkerbot/3812841 to your computer and use it in GitHub Desktop.
Save thinkerbot/3812841 to your computer and use it in GitHub Desktop.
Factory Girl study
require 'factory_girl'
# Build does not go through initializer :(
# class Receiver
# attr_reader :calls
# def initializer
# @calls = []
# end
# def a=(arg)
# @calls << arg
# end
# def b=(arg)
# @calls << arg
# end
# end
class Receiver
def calls
@calls ||= []
end
def a=(arg)
calls << arg
end
def b=(arg)
calls << arg
end
def c=(arg)
calls << arg
end
def x=(arg)
calls << arg
end
def y=(arg)
calls << arg
end
def z=(arg)
calls << arg
end
end
FactoryGirl.define do
factory :receiver do
a 0
b :B
x 1
y :Y
end
factory :receiver_alt, :class => Receiver do
a 0
x 1
b :B
y :Y
end
end
#
# Assignment order is determined for declared attributes. Not guaranteed for
# undeclared attributes.
#
# [:A, :B, :X, :Y, :Z, :C]
10.times do
r = FactoryGirl.build(:receiver, :a => :A, :c => :C, :x => :X, :z => :Z)
puts r.calls.inspect
end
# [:A, :X, :B, :Y, :Z, :C]
10.times do
r = FactoryGirl.build(:receiver_alt, :a => :A, :c => :C, :x => :X, :z => :Z)
puts r.calls.inspect
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment