Permalink
Browse files

Allow implicit syntax methods in callbacks

  • Loading branch information...
1 parent ff0e6fb commit 327e0c1f2b8d4f83ef2a1393a325549daabb7dfb @joshuaclayton joshuaclayton committed May 2, 2012
Showing with 38 additions and 3 deletions.
  1. +7 −3 lib/factory_girl/callback.rb
  2. +31 −0 spec/acceptance/callbacks_spec.rb
View
10 lib/factory_girl/callback.rb
@@ -10,9 +10,9 @@ def initialize(name, block)
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 @@ def check_name
"Valid callback names are #{FactoryGirl.callback_names.inspect}"
end
end
+
+ def syntax_runner
+ @syntax_runner ||= SyntaxRunner.new
+ end
end
end
View
31 spec/acceptance/callbacks_spec.rb
@@ -45,3 +45,34 @@
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

0 comments on commit 327e0c1

Please sign in to comment.