Permalink
Browse files

Build an association without arguments in a definition proxy

  • Loading branch information...
jferris committed Jul 7, 2010
1 parent bf0b6a6 commit c7bb677777d84a9548f6a71985ce48b0a816481a
@@ -26,7 +26,7 @@ def initialize(factory)
# generated instances.
# * value: +Object+
# If no block is given, this value will be used for this attribute.
- def add_attribute (name, value = nil, &block)
+ def add_attribute(name, value = nil, &block)
if block_given?
if value
raise AttributeDefinitionError, "Both value and block given"
@@ -54,8 +54,12 @@ def add_attribute (name, value = nil, &block)
# end
#
# are equivilent.
- def method_missing (name, *args, &block)
- add_attribute(name, *args, &block)
+ def method_missing(name, *args, &block)
+ if args.empty? && block.nil?
+ association(name)
+ else
+ add_attribute(name, *args, &block)
+ end
end
# Adds an attribute that will have unique values generated by a sequence with
@@ -11,16 +11,21 @@
email {|a| "#{a.first_name}.#{a.last_name}@example.com".downcase }
end
+ # TODO: add sugar for this
+ factory :author, :parent => :user do
+ end
+
factory Post, :default_strategy => :attributes_for do
name 'Test Post'
- association :author, :factory => :user
+ author
end
factory :admin, :class => User do
first_name 'Ben'
last_name 'Stein'
admin true
sequence(:username) { |n| "username#{n}" }
+ # TODO: add sugar for this
email { Factory.next(:email) }
end
@@ -5,12 +5,12 @@
subject { FactoryGirl::DefinitionProxy.new(factory) }
it "should add a static attribute for type" do
- subject.type
+ subject.type 'value'
factory.attributes.last.should be_kind_of(FactoryGirl::Attribute::Static)
end
it "should add a static attribute for id" do
- subject.id
+ subject.id 'value'
factory.attributes.last.should be_kind_of(FactoryGirl::Attribute::Static)
end
@@ -97,9 +97,26 @@
it "should add an attribute using the method name when passed an undefined method" do
attribute = 'attribute'
stub(attribute).name { :name }
- block = lambda {}
mock(FactoryGirl::Attribute::Static).new(:name, 'value') { attribute }
subject.send(:name, 'value')
factory.attributes.should include(attribute)
end
+
+ it "adds an attribute using when passed an undefined method and block" do
+ attribute = 'attribute'
+ stub(attribute).name { :name }
+ block = lambda {}
+ mock(FactoryGirl::Attribute::Dynamic).new(:name, block) { attribute }
+ subject.send(:name, &block)
+ factory.attributes.should include(attribute)
+ end
+
+ it "adds an association when passed an undefined method without arguments or a block" do
+ name = :user
+ attr = 'attribute'
+ stub(attr).name { name }
+ mock(FactoryGirl::Attribute::Association).new(name, name, {}) { attr }
+ subject.send(name)
+ factory.attributes.should include(attr)
+ end
end

0 comments on commit c7bb677

Please sign in to comment.