diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..d404977 --- /dev/null +++ b/Gemfile.lock @@ -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) diff --git a/README.rdoc b/README.rdoc index 44964df..b527475 100644 --- a/README.rdoc +++ b/README.rdoc @@ -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 @@ -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 diff --git a/lib/cranky.rb b/lib/cranky.rb index 3dd9fe2..8596cad 100644 --- a/lib/cranky.rb +++ b/lib/cranky.rb @@ -12,7 +12,7 @@ class Cranky # end # end - attr_accessor :debug + attr_writer :debug def initialize @what = [] @@ -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 diff --git a/spec/cranky_spec.rb b/spec/cranky_spec.rb index 7246d96..f3ba1ab 100644 --- a/spec/cranky_spec.rb +++ b/spec/cranky_spec.rb @@ -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