Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Build an association without arguments in a definition proxy

  • Loading branch information...
commit c7bb677777d84a9548f6a71985ce48b0a816481a 1 parent bf0b6a6
Joe Ferris authored July 06, 2010
10  lib/factory_girl/definition_proxy.rb
@@ -26,7 +26,7 @@ def initialize(factory)
26 26
     #   generated instances.
27 27
     # * value: +Object+
28 28
     #   If no block is given, this value will be used for this attribute.
29  
-    def add_attribute (name, value = nil, &block)
  29
+    def add_attribute(name, value = nil, &block)
30 30
       if block_given?
31 31
         if value
32 32
           raise AttributeDefinitionError, "Both value and block given"
@@ -54,8 +54,12 @@ def add_attribute (name, value = nil, &block)
54 54
     #   end
55 55
     #
56 56
     # are equivilent.
57  
-    def method_missing (name, *args, &block)
58  
-      add_attribute(name, *args, &block)
  57
+    def method_missing(name, *args, &block)
  58
+      if args.empty? && block.nil?
  59
+        association(name)
  60
+      else
  61
+        add_attribute(name, *args, &block)
  62
+      end
59 63
     end
60 64
 
61 65
     # Adds an attribute that will have unique values generated by a sequence with
7  spec/acceptance/acceptance_spec.rb
@@ -11,9 +11,13 @@
11 11
         email {|a| "#{a.first_name}.#{a.last_name}@example.com".downcase }
12 12
       end
13 13
 
  14
+      # TODO: add sugar for this
  15
+      factory :author, :parent => :user do
  16
+      end
  17
+
14 18
       factory Post, :default_strategy => :attributes_for do
15 19
         name   'Test Post'
16  
-        association :author, :factory => :user
  20
+        author
17 21
       end
18 22
 
19 23
       factory :admin, :class => User do
@@ -21,6 +25,7 @@
21 25
         last_name  'Stein'
22 26
         admin       true
23 27
         sequence(:username) { |n| "username#{n}" }
  28
+        # TODO: add sugar for this
24 29
         email { Factory.next(:email) }
25 30
       end
26 31
 
23  spec/factory_girl/definition_proxy_spec.rb
@@ -5,12 +5,12 @@
5 5
   subject { FactoryGirl::DefinitionProxy.new(factory) }
6 6
 
7 7
   it "should add a static attribute for type" do
8  
-    subject.type
  8
+    subject.type 'value'
9 9
     factory.attributes.last.should be_kind_of(FactoryGirl::Attribute::Static)
10 10
   end
11 11
 
12 12
   it "should add a static attribute for id" do
13  
-    subject.id
  13
+    subject.id 'value'
14 14
     factory.attributes.last.should be_kind_of(FactoryGirl::Attribute::Static)
15 15
   end
16 16
 
@@ -97,9 +97,26 @@
97 97
   it "should add an attribute using the method name when passed an undefined method" do
98 98
     attribute = 'attribute'
99 99
     stub(attribute).name { :name }
100  
-    block = lambda {}
101 100
     mock(FactoryGirl::Attribute::Static).new(:name, 'value') { attribute }
102 101
     subject.send(:name, 'value')
103 102
     factory.attributes.should include(attribute)
104 103
   end
  104
+
  105
+  it "adds an attribute using when passed an undefined method and block" do
  106
+    attribute = 'attribute'
  107
+    stub(attribute).name { :name }
  108
+    block = lambda {}
  109
+    mock(FactoryGirl::Attribute::Dynamic).new(:name, block) { attribute }
  110
+    subject.send(:name, &block)
  111
+    factory.attributes.should include(attribute)
  112
+  end
  113
+
  114
+  it "adds an association when passed an undefined method without arguments or a block" do
  115
+    name = :user
  116
+    attr = 'attribute'
  117
+    stub(attr).name { name }
  118
+    mock(FactoryGirl::Attribute::Association).new(name, name, {}) { attr }
  119
+    subject.send(name)
  120
+    factory.attributes.should include(attr)
  121
+  end
105 122
 end

0 notes on commit c7bb677

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