1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00

Evaulate dynamic attribute blocks without arguments in the context of the proxy

This commit is contained in:
Joe Ferris 2010-09-30 23:35:24 -04:00
parent 3bdd6a3eff
commit 58ee31b91a
3 changed files with 13 additions and 3 deletions

View file

@ -8,7 +8,7 @@ module FactoryGirl
end
def add_to(proxy)
value = @block.arity.zero? ? @block.call : @block.call(proxy)
value = @block.arity == 1 ? @block.call(proxy) : proxy.instance_eval(&@block)
if FactoryGirl::Sequence === value
raise SequenceAbuseError
end

View file

@ -8,8 +8,7 @@ describe "integration" do
first_name 'Jimi'
last_name 'Hendrix'
admin false
# TODO: evaluate in context of proxy
email {|a| "#{a.first_name}.#{a.last_name}@example.com".downcase }
email { "#{first_name}.#{last_name}@example.com".downcase }
# TODO: nested factories

View file

@ -27,6 +27,17 @@ describe FactoryGirl::Attribute::Dynamic do
@proxy.should have_received.set(:user, @proxy)
end
it "evaluates the block with in the context of the proxy without an argument" do
result = 'other attribute value'
@block = lambda { other_attribute }
@attr = FactoryGirl::Attribute::Dynamic.new(:user, @block)
@proxy = "proxy"
stub(@proxy).set
stub(@proxy).other_attribute { result }
@attr.add_to(@proxy)
@proxy.should have_received.set(:user, result)
end
it "should raise an error when defining an attribute writer" do
lambda {
FactoryGirl::Attribute::Dynamic.new('test=', nil)