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

Allow implicit syntax methods in callbacks

This commit is contained in:
Joshua Clayton 2012-05-02 00:56:20 -04:00
parent ff0e6fb6c0
commit 327e0c1f2b
2 changed files with 38 additions and 3 deletions

View file

@ -10,9 +10,9 @@ module FactoryGirl
def run(instance, evaluator)
case block.arity
when 1 then block.call(instance)
when 2 then block.call(instance, evaluator)
else block.call
when 1 then syntax_runner.instance_exec(instance, &block)
when 2 then syntax_runner.instance_exec(instance, evaluator, &block)
else syntax_runner.instance_exec(&block)
end
end
@ -32,5 +32,9 @@ module FactoryGirl
"Valid callback names are #{FactoryGirl.callback_names.inspect}"
end
end
def syntax_runner
@syntax_runner ||= SyntaxRunner.new
end
end
end

View file

@ -45,3 +45,34 @@ describe "callbacks" do
user.first_name.should == 'Child-Buildy'
end
end
describe "callbacks using syntax methods without referencing FactoryGirl explicitly" do
before do
define_model("User", first_name: :string, last_name: :string)
FactoryGirl.define do
sequence(:sequence_1)
sequence(:sequence_2)
sequence(:sequence_3)
factory :user do
after_stub { generate(:sequence_3) }
after_build {|user| user.first_name = generate(:sequence_1) }
after_create {|user, evaluator| user.last_name = generate(:sequence_2) }
end
end
end
it "works when the callback has no variables" do
FactoryGirl.build_stubbed(:user)
FactoryGirl.generate(:sequence_3).should == 2
end
it "works when the callback has one variable" do
FactoryGirl.build(:user).first_name.should == 1
end
it "works when the callback has two variables" do
FactoryGirl.create(:user).last_name.should == 1
end
end