Skip to content
Browse files

Added factory aliases

  • Loading branch information...
1 parent c7bb677 commit 3bdd6a3efff6fc7bc50f6ce0012dec359e979573 @jferris jferris committed Sep 7, 2010
View
18 lib/factory_girl/definition_proxy.rb
@@ -111,6 +111,24 @@ def association(name, options = {})
@factory.define_attribute(Attribute::Association.new(name, factory_name, options))
end
+ # Registers an alias for this factory using the given name.
+ #
+ # Arguments:
+ # * name: +Symbol+
+ # The name of the alias.
+ #
+ # Example:
+ #
+ # Factory.define :user do |f|
+ # f.aliased_as :author
+ # end
+ #
+ # Factory(:author).class
+ # # => User
+ def aliased_as(name)
+ FactoryGirl.register_factory(@factory, :as => name)
+ end
+
def after_build(&block)
@factory.add_callback(:after_build, &block)
end
View
4 lib/factory_girl/factory.rb
@@ -9,8 +9,8 @@ def self.factory_by_name(name)
factories[name.to_sym] or raise ArgumentError.new("No such factory: #{name.to_s}")
end
- def self.register_factory(factory)
- name = factory.factory_name
+ def self.register_factory(factory, options = {})
+ name = options[:as] || factory.factory_name
if self.factories[name]
raise DuplicateDefinitionError, "Factory already defined: #{name}"
end
View
9 spec/acceptance/acceptance_spec.rb
@@ -8,18 +8,20 @@
first_name 'Jimi'
last_name 'Hendrix'
admin false
+ # TODO: evaluate in context of proxy
email {|a| "#{a.first_name}.#{a.last_name}@example.com".downcase }
- end
- # TODO: add sugar for this
- factory :author, :parent => :user do
+ # TODO: nested factories
+
+ aliased_as :author
end
factory Post, :default_strategy => :attributes_for do
name 'Test Post'
author
end
+ # TODO: syntax for build classes
factory :admin, :class => User do
first_name 'Ben'
last_name 'Stein'
@@ -39,6 +41,7 @@
end
factory :user_with_callbacks, :parent => :user do
+ # TODO: evaluate in context of instance
after_stub {|u| u.first_name = 'Stubby' }
after_build {|u| u.first_name = 'Buildy' }
after_create {|u| u.last_name = 'Createy' }
View
8 spec/factory_girl/definition_proxy_spec.rb
@@ -47,7 +47,7 @@
mock(FactoryGirl::Sequence).new("A")
subject.sequence(:name, "A") {}
end
-
+
it "should add a dynamic attribute" do
attribute = 'attribute'
stub(attribute).name { :name }
@@ -119,4 +119,10 @@
subject.send(name)
factory.attributes.should include(attr)
end
+
+ it "registers its factory for an alias" do
+ aliased_name = :guest
+ mock(FactoryGirl).register_factory(factory, :as => aliased_name)
+ subject.aliased_as aliased_name
+ end
end
View
14 spec/factory_girl/factory_spec.rb
@@ -299,6 +299,20 @@
end
end
+describe FactoryGirl::Factory, "registered with a custom name" do
+ before do
+ @actual_name = :string
+ @custom_name = :words
+ @factory = FactoryGirl::Factory.new(@actual_name)
+
+ FactoryGirl.register_factory(@factory, :as => @custom_name)
+ end
+
+ it "finds the factory using the custom name" do
+ FactoryGirl.factory_by_name(@custom_name).should == @factory
+ end
+end
+
describe FactoryGirl::Factory, "for namespaced class" do
include DefinesConstants

0 comments on commit 3bdd6a3

Please sign in to comment.
Something went wrong with that request. Please try again.