diff --git a/lib/cranky/factory.rb b/lib/cranky/factory.rb index 975a3e2..87ee72a 100644 --- a/lib/cranky/factory.rb +++ b/lib/cranky/factory.rb @@ -41,11 +41,12 @@ def debug(*args) if !item.valid? raise "Oops, the #{item.class} created by the Factory has the following errors: #{item.errors}" end + item end # Same thing for create def debug!(*args) - item = debug + item = debug(*args) item.save item end diff --git a/spec/cranky_spec.rb b/spec/cranky_spec.rb index c87954b..ba30605 100644 --- a/spec/cranky_spec.rb +++ b/spec/cranky_spec.rb @@ -75,13 +75,19 @@ it "should raise an error if the factory produces an invalid object when enabled (rails only)" do error = false begin - Factory.debug(:user) + Factory.debug(:user, :valid => false) rescue error = true end error.should == true end + it "should have debug work like build and create when there are no errors" do + Factory.debug(:user).class.should == User + Factory.debug(:user).saved?.should == false + Factory.debug!(:user).saved?.should == true + end + end it "should allow arguments to be passed in the overrides hash" do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b6af0e1..b320d6a 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -4,6 +4,8 @@ class TestClass + attr_accessor :valid + def save @saved = true end @@ -13,7 +15,7 @@ def saved? end def valid? - false + @valid end def errors @@ -53,6 +55,7 @@ def user_manually u.unique = "value#{n}" u.email = "fred@home.com" u.address = Factory.build(:address) + u.valid = true u end @@ -62,7 +65,8 @@ def user_by_define :role => :user, :unique => "value#{n}", :email => "fred@home.com", - :address => Factory.create(:address) + :address => Factory.create(:address), + :valid => true u.argument_received = true if options[:argument_supplied] u end @@ -78,7 +82,8 @@ def admin_by_define def address define :address => "25 Wisteria Lane", - :city => "New York" + :city => "New York", + :valid => true end end