Permalink
Browse files

Ensure the yielded value is returned when block is given.

In the following example, `FactoryGirl.create(:order)` returns 0 because the last expression returns 0.
`:order` is set to 0 causing an unexpected behavior.

    let(:order) {
      FactoryGirl.create(:order) do |order|
        order.save!
        order.total_cents = 0
      end
    }

    order
    # => 0
  • Loading branch information...
1 parent 7254a81 commit 4b6ada72cfe551becf13e9e707d1591833379075 @weppos weppos committed with joshuaclayton Dec 20, 2011
@@ -42,7 +42,7 @@ def run(proxy_class, overrides, &block) #:nodoc:
evaluator = evaluator_class_definer.evaluator_class.new(proxy, overrides.symbolize_keys, callbacks)
attribute_assigner = AttributeAssigner.new(build_class, evaluator, attributes)
- block[proxy.result(attribute_assigner, to_create)]
+ proxy.result(attribute_assigner, to_create).tap(&block)
end
def human_names
@@ -61,6 +61,14 @@
attributes[:name].should eq('thoughtbot')
end
end
+
+ it "returns the hash of attributes" do
+ expected = nil
+ attributes_for(:company) do |attributes|
+ expected = attributes
+ "hello!"
+ end.should == expected
+ end
end
describe "`attributes_for` for a class whose constructor has required params" do
@@ -78,4 +78,12 @@
company.name.should eq('thoughtbot')
end
end
+
+ it "returns the built instance" do
+ expected = nil
+ build(:company) do |company|
+ expected = company
+ "hello!"
+ end.should == expected
+ end
end
@@ -92,4 +92,12 @@
expect { company.save }.to raise_error(RuntimeError)
end
end
+
+ it "returns the stub instance" do
+ expected = nil
+ build_stubbed(:company) do |company|
+ expected = company
+ "hello!"
+ end.should == expected
+ end
end
@@ -106,4 +106,12 @@ def persisted?
company.name.should eq('thoughtbot')
end
end
+
+ it "returns the created instance" do
+ expected = nil
+ create(:company) do |company|
+ expected = company
+ "hello!"
+ end.should == expected
+ end
end

0 comments on commit 4b6ada7

Please sign in to comment.