Skip to content

Commit

Permalink
Bug fix to debug method not returning created object, added test cove…
Browse files Browse the repository at this point in the history
…rage and fixed
  • Loading branch information
Ginty committed Aug 22, 2010
1 parent cf1b814 commit abc1886
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
3 changes: 2 additions & 1 deletion lib/cranky/factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion spec/cranky_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@


class TestClass
attr_accessor :valid

def save
@saved = true
end
Expand All @@ -13,7 +15,7 @@ def saved?
end

def valid?
false
@valid
end

def errors
Expand Down Expand Up @@ -53,6 +55,7 @@ def user_manually
u.unique = "value#{n}"
u.email = "[email protected]"
u.address = Factory.build(:address)
u.valid = true
u
end

Expand All @@ -62,7 +65,8 @@ def user_by_define
:role => :user,
:unique => "value#{n}",
:email => "[email protected]",
:address => Factory.create(:address)
:address => Factory.create(:address),
:valid => true
u.argument_received = true if options[:argument_supplied]
u
end
Expand All @@ -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
Expand Down

0 comments on commit abc1886

Please sign in to comment.