Skip to content

Commit

Permalink
Improved the debugger syntax and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ginty committed Aug 14, 2010
1 parent fffd670 commit 455aa9b
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 10 deletions.
14 changes: 14 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
GEM
remote: http://gemcutter.org/
specs:
gemcutter (0.6.1)
rcov (0.9.8)
rspec (1.3.0)

PLATFORMS
ruby

DEPENDENCIES
gemcutter
rcov
rspec (= 1.3.0)
19 changes: 19 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ Cranky steals its core syntax from Factory Girl...
Factory.build(:user, :name => "Ian") # Override a default attribute value
Factory.attributes_for(:user) # Return a set of valid attributes rather than the object

And has a nice debug option (rails only) to warn you when your factory is broken, recommend you do this for your first spec...

describe User do
it "should have a working factory" do
# This will raise an error and report the validation errors should they occur
Factory.debug(:user).should be_valid
end
end

Cranky allows you to build factories via std Ruby methods, like this...

# factories/my_factories.rb
Expand Down Expand Up @@ -226,6 +235,16 @@ Sometimes it is useful to be warned that your factory is generating invalid inst

Factory.debug = true

Or run within a block...

Factory.debug do
Factory.build(:user)
end

Or inline (runs the build method with debug enabled)...

Factory.debug(:user)

Note that this relies on the instance having a valid? method, so in practice this may only work with Rails.

=== Attributes For
Expand Down
17 changes: 16 additions & 1 deletion lib/cranky.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Cranky
# end
# end

attr_accessor :debug
attr_writer :debug

def initialize
@what = []
Expand All @@ -39,6 +39,21 @@ def attributes_for(what, attrs={})
build(what, attrs).attributes
end

def debug(what=nil)
if block_given?
@debug = true
yield
@debug = false
elsif what
@debug = true
item = build(what)
@debug = false
item
else
@debug
end
end

private

def n
Expand Down
47 changes: 38 additions & 9 deletions spec/cranky_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,45 @@
b.unique.should_not == c.unique
end

it "should raise an error if the factory produces an invalid object when debug is enabled (rails only)" do
Factory.debug = true
error = false
begin
Factory.build(:user)
rescue
error = true
describe "debugger" do

it "should raise an error if the factory produces an invalid object when enabled (rails only)" do
Factory.debug = true
error = false
begin
Factory.build(:user)
rescue
error = true
end
error.should == true
Factory.debug = false
end

it "can be run as a block" do
Factory.debug.should == false
error = false
Factory.debug do
begin
Factory.build(:user)
rescue
error = true
end
end
error.should == true
Factory.debug.should == false
end
error.should == true
Factory.debug = false

it "can be run inline" do
Factory.debug.should == false
error = false
begin
Factory.debug(:user)
rescue
error = true
end
error.should == true
end

end

it "should allow arguments to be passed in the overrides hash" do
Expand Down

0 comments on commit 455aa9b

Please sign in to comment.