Attempting to define multiple factories with the same name will raise an
error.
This commit is contained in:
parent
2b94af062c
commit
1e556bfe09
|
@ -43,6 +43,8 @@ Each factory has a name and a set of attributes. The name is used to guess the c
|
|||
|
||||
It is highly recommended that you have one factory for each class that provides the simplest set of attributes necessary to create an instance of that class. If you're creating ActiveRecord objects, that means that you should only provide attributes that are required through validations and that do not have defaults. Other factories can be created through inheritance to cover common scenarios for each class.
|
||||
|
||||
Attempting to define multiple factories with the same name will raise an error.
|
||||
|
||||
Factories can either be defined anywhere, but will automatically be loaded if they are defined in files at the following locations:
|
||||
|
||||
test/factories.rb
|
||||
|
|
|
@ -7,6 +7,10 @@ class Factory
|
|||
# Raised when a callback is defined that has an invalid name
|
||||
class InvalidCallbackNameError < RuntimeError
|
||||
end
|
||||
|
||||
# Raised when a factory is defined with the same name as a previously-defined factory.
|
||||
class DuplicateDefinitionError < RuntimeError
|
||||
end
|
||||
|
||||
class << self
|
||||
attr_accessor :factories #:nodoc:
|
||||
|
@ -51,6 +55,9 @@ class Factory
|
|||
if parent = options.delete(:parent)
|
||||
instance.inherit_from(Factory.factory_by_name(parent))
|
||||
end
|
||||
if self.factories[instance.factory_name]
|
||||
raise DuplicateDefinitionError, "Factory already defined: #{name}"
|
||||
end
|
||||
self.factories[instance.factory_name] = instance
|
||||
end
|
||||
|
||||
|
|
|
@ -10,6 +10,8 @@ describe Factory do
|
|||
stub(Factory).new { @factory }
|
||||
end
|
||||
|
||||
after { Factory.factories.clear }
|
||||
|
||||
it "should create a new factory using the specified name and options" do
|
||||
mock(Factory).new(@name, @options) { @factory }
|
||||
Factory.define(@name, @options) {|f| }
|
||||
|
@ -28,9 +30,17 @@ describe Factory do
|
|||
@factory.should == Factory.factories[@name]
|
||||
end
|
||||
|
||||
it "should allow that factory to be found by name" do
|
||||
it "should allow a factory to be found by name" do
|
||||
Factory.define(@name) {|f| }
|
||||
Factory.factory_by_name(@name).should == @factory
|
||||
end
|
||||
|
||||
it "should not allow a duplicate factory definition" do
|
||||
lambda {
|
||||
2.times { Factory.define(@name) {|f| } }
|
||||
}.should raise_error(Factory::DuplicateDefinitionError)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe "a factory" do
|
||||
|
@ -420,6 +430,8 @@ describe Factory do
|
|||
end
|
||||
end
|
||||
|
||||
after { Factory.factories.clear }
|
||||
|
||||
it "should raise an ArgumentError when trying to use a non-existent factory as parent" do
|
||||
lambda {
|
||||
Factory.define(:child, :parent => :nonexsitent) {}
|
||||
|
|
Loading…
Reference in New Issue