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:
parent
3bdd6a3eff
commit
58ee31b91a
3 changed files with 13 additions and 3 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Reference in a new issue