Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Remove default fabricators and throw an error instead

  • Loading branch information...
commit 1db2ad763a487addeb90f0447618d4302bbcd4e8 1 parent c50e1c4
Paul Elliott authored
View
1  Changelog.markdown
@@ -1,6 +1,7 @@
### 0.7.1 (09/12/2010) ###
* Looks for Rails root when searching for Fabricators
+* Remove default fabricators. All definitions must be explicit!
### 0.7.0 (09/08/2010) ###
View
21 README.markdown
@@ -10,6 +10,14 @@ Currently supported object types are...
By default it will lazily generate active record associations. So if you have a has_many :widgets defined, it will not actually generate the widgets until the association is accessed. You can override this by appending "!" to the name of the parameter when defining the field in the Fabricator.
+### Important Thing To Note! ###
+
+If you are fabricating an activerecord backed object and it has attributes that are not columns in the underlying table, Fabrication will lazily generate them even if they are not defined associations. You can easily work around this by adding a "!" to the end of the attribute definition in the Fabricator.
+
+Fabricator(:user) do
+ some_delegated_something_or_other! { Fabricate(:something) }
+end
+
### Installation ###
Add this to your gemfile.
@@ -46,16 +54,6 @@ Breaking down the above, we are defining a "company" fabricator, which will gene
* After the object is built but before it is saved, it will update the name to "Another Fun Factory".
* After the object is created, it will update the "ceo" association with a new "drone" record.
-Alternatively, you can Fabricate(:company) without first defining the Fabricator. Doing so will create an empty Fabricator called ":company" and prevent you from defining the Fabricator explicitly later.
-
-### Important Thing To Note! ###
-
-If you are fabricating an activerecord backed object and it has attributes that are not columns in the underlying table, Fabrication will lazily generate them even if they are not defined associations. You can easily work around this by adding a "!" to the end of the attribute definition in the Fabricator.
-
-Fabricator(:user) do
- some_delegated_something_or_other! { Fabricate(:something) }
-end
-
### Inheritance ###
So you already have a company fabricator, but you need one that specifically generates an LLC. No problem!
@@ -64,7 +62,7 @@ So you already have a company fabricator, but you need one that specifically gen
type "LLC"
end
-Setting the :from option will inherit the class and all the attributes from the named Fabricator. Even if you haven't defined a :company Fabricator yet, it will still work as long as it references an actual class name.
+Setting the :from option will inherit the class and all the attributes from the named Fabricator.
You can also explicitly specify the class being fabricated with the :class_name parameter.
@@ -136,3 +134,4 @@ To run rake successfully:
* Dave Ott (daveott)
* Matt (winescout)
* Lar Van Der Jagt (supaspoida)
+* Justin Smestad (jsmestad)
View
2  lib/fabrication/errors.rb
@@ -4,4 +4,6 @@ class DuplicateFabricatorError < StandardError; end
class UnfabricatableError < StandardError; end
+ class UnknownFabricatorError < StandardError; end
+
end
View
3  lib/fabrication/fabricator.rb
@@ -9,7 +9,8 @@ def define(name, options={}, &block)
def generate(name, options={}, overrides={}, &block)
Fabrication::Support.find_definitions if schematics.empty?
- (schematics[name] || define(name)).generate(options, overrides, &block)
+ raise Fabrication::UnknownFabricatorError, "No Fabricator defined for '#{name}'" unless schematics.has_key?(name)
+ schematics[name].generate(options, overrides, &block)
end
def schematics
View
11 spec/fabrication/fabricator_spec.rb
@@ -41,19 +41,22 @@
it "finds definitions if none exist" do
Fabrication::Support.should_receive(:find_definitions)
- subject.generate(:object)
+ lambda { subject.generate(:object) }.should raise_error
end
end
context 'with definitions' do
- it "raises an error if the object can't be fabricated" do
- lambda { subject.generate(:somenonexistantclass) }.should raise_error(Fabrication::UnfabricatableError)
+ it "raises an error if the class cannot be located" do
+ lambda { subject.define(:somenonexistantclass) }.should raise_error(Fabrication::UnfabricatableError)
+ end
+
+ it "raises an error if the fabricator cannot be located" do
+ lambda { subject.generate(:object) }.should raise_error(Fabrication::UnknownFabricatorError)
end
it 'generates a new object every time' do
- subject.define(:person) { first_name '1' }
subject.generate(:person).should_not == subject.generate(:person)
end
View
10 spec/fabrication_spec.rb
@@ -275,10 +275,18 @@
end
+ context "when defining a fabricator for a class that doesn't exist" do
+
+ it 'throws an error' do
+ lambda { Fabricator(:your_mom) }.should raise_error(Fabrication::UnfabricatableError)
+ end
+
+ end
+
context 'when generating from a non-existant fabricator' do
it 'throws an error' do
- lambda { Fabricate(:your_mom) }.should raise_error(Fabrication::UnfabricatableError)
+ lambda { Fabricate(:your_mom) }.should raise_error(Fabrication::UnknownFabricatorError)
end
end
View
1  spec/fabricators.rb
@@ -1,6 +1,7 @@
# Plain Ruby Objects
Fabricator(:awesome_object, :from => :object)
+Fabricator(:dog)
Fabricator(:greyhound, :from => :dog) do
breed "greyhound"
locations(:count => 2)
Please sign in to comment.
Something went wrong with that request. Please try again.