factory_girl 2.5.0 → 2.5.1

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml CHANGED
@@ -3,6 +3,8 @@ rvm:
3
3
  - 1.8.7
4
4
  - 1.9.2
5
5
  - 1.9.3
6
+ before_install:
7
+ - gem update --system
6
8
  script: "bundle exec rake spec:unit spec:acceptance features"
7
9
  gemfile:
8
10
  - gemfiles/2.1.gemfile
data/Changelog CHANGED
@@ -1,3 +1,12 @@
1
+ 2.5.1 (February 3, 2012)
2
+ Fix attribute evaluation when the attribute isn't defined in the factory but
3
+ is a private method on Object
4
+ Update rubygems on Travis before running tests
5
+ Fix spec name
6
+ Update GETTING_STARTED with correct usage of build_stubbed
7
+ Update README with more info on initialize_with
8
+ Honor :parent on factory over block nesting
9
+
1
10
  2.5.0 (January 20, 2012)
2
11
  Revert 'Deprecate build_stubbed and attributes_for'
3
12
  Implement initialize_with to allow overriding object instantiation
data/GETTING_STARTED.md CHANGED
@@ -487,7 +487,7 @@ factory\_girl makes available three callbacks for injecting some code:
487
487
 
488
488
  * after_build - called after a factory is built (via FactoryGirl.build)
489
489
  * after_create - called after a factory is saved (via FactoryGirl.create)
490
- * after_stub - called after a factory is stubbed (via FactoryGirl.stub)
490
+ * after_stub - called after a factory is stubbed (via FactoryGirl.build_stubbed)
491
491
 
492
492
  Examples:
493
493
 
@@ -593,10 +593,10 @@ twenty_year_olds = FactoryGirl.build_list(:user, 25, :date_of_birth => 20.years.
593
593
  Custom Construction
594
594
  -------------------
595
595
 
596
- Instantiating objects can be overridden in the case where you'd rather not
597
- call `new` on your build class or you have some other factory method that
598
- you'd prefer to use. Using custom construction also allows for your objects to
599
- be instantiated with any number of arguments.
596
+ If you want to use factory_girl to construct an object where some attributes
597
+ are passed to `initialize` or if you want to do something other than simply
598
+ calling `new` on your build class, you can override the default behavior by
599
+ defining `to_initialize` on your factory. Example:
600
600
 
601
601
  ```ruby
602
602
  # user.rb
@@ -626,6 +626,19 @@ FactoryGirl.build(:user).name # Bob Hope
626
626
  Notice that I ignored the `name` attribute. If you don't want attributes
627
627
  reassigned after your object has been instantiated, you'll want to `ignore` them.
628
628
 
629
+ Although factory_girl is written to work with ActiveRecord out of the box, it
630
+ can also work with any Ruby class. For maximum compatibiltiy with ActiveRecord,
631
+ the default initializer builds all instances by calling new on your build class
632
+ without any arguments. It then calls attribute writer methods to assign all the
633
+ attribute values. While that works fine for ActiveRecord, it actually doesn't
634
+ work for almost any other Ruby class.
635
+
636
+ You can override the initializer in order to:
637
+
638
+ * Build non-ActiveRecord objects that require arguments to `initialize`
639
+ * Use a method other than `new` to instantiate the instance
640
+ * Do crazy things like decorate the instance after it's built
641
+
629
642
  Cucumber Integration
630
643
  --------------------
631
644
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- factory_girl (2.5.0)
4
+ factory_girl (2.5.1)
5
5
  activesupport
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/joshuaclayton/dev/gems/factory_girl
3
3
  specs:
4
- factory_girl (2.5.0)
4
+ factory_girl (2.5.1)
5
5
  activesupport
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/joshuaclayton/dev/gems/factory_girl
3
3
  specs:
4
- factory_girl (2.5.0)
4
+ factory_girl (2.5.1)
5
5
  activesupport
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/joshuaclayton/dev/gems/factory_girl
3
3
  specs:
4
- factory_girl (2.5.0)
4
+ factory_girl (2.5.1)
5
5
  activesupport
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/joshuaclayton/dev/gems/factory_girl
3
3
  specs:
4
- factory_girl (2.5.0)
4
+ factory_girl (2.5.1)
5
5
  activesupport
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/joshuaclayton/dev/gems/factory_girl
3
3
  specs:
4
- factory_girl (2.5.0)
4
+ factory_girl (2.5.1)
5
5
  activesupport
6
6
 
7
7
  GEM
@@ -11,13 +11,21 @@ module FactoryGirl
11
11
  end
12
12
  end
13
13
  end
14
- undef_method(:id) if method_defined?(:id)
14
+
15
+ private_instance_methods.each do |method|
16
+ undef_method(method) unless method =~ /^__|initialize/
17
+ end
15
18
 
16
19
  def initialize(build_strategy, overrides = {})
17
20
  @build_strategy = build_strategy
18
21
  @overrides = overrides
19
22
  @cached_attributes = overrides
20
23
 
24
+ singleton = class << self; self end
25
+ @overrides.each do |name, value|
26
+ singleton.send :define_method, name, lambda { value }
27
+ end
28
+
21
29
  @build_strategy.add_observer(CallbackRunner.new(self.class.callbacks, self))
22
30
  end
23
31
 
@@ -24,7 +24,8 @@ module FactoryGirl
24
24
  FactoryGirl.register_factory(factory)
25
25
 
26
26
  proxy.child_factories.each do |(child_name, child_options, child_block)|
27
- factory(child_name, child_options.merge(:parent => name), &child_block)
27
+ parent_factory = child_options.delete(:parent) || name
28
+ factory(child_name, child_options.merge(:parent => parent_factory), &child_block)
28
29
  end
29
30
  end
30
31
 
@@ -1,4 +1,4 @@
1
1
  module FactoryGirl
2
- VERSION = "2.5.0"
2
+ VERSION = "2.5.1"
3
3
  end
4
4
 
@@ -0,0 +1,68 @@
1
+ require "spec_helper"
2
+
3
+ describe "declaring attributes on a Factory that are private methods on Object" do
4
+ before do
5
+ define_model("Website", :system => :boolean, :link => :string, :sleep => :integer)
6
+
7
+ FactoryGirl.define do
8
+ factory :website do
9
+ system false
10
+ link "http://example.com"
11
+ sleep 15
12
+ end
13
+ end
14
+ end
15
+
16
+ subject { FactoryGirl.build(:website, :sleep => -5) }
17
+
18
+ its(:system) { should == false }
19
+ its(:link) { should == "http://example.com" }
20
+ its(:sleep) { should == -5 }
21
+ end
22
+
23
+ describe "assigning overrides that are also private methods on object" do
24
+ before do
25
+ define_model("Website", :format => :string, :y => :integer, :more_format => :string, :some_funky_method => :string)
26
+
27
+ Object.class_eval do
28
+ private
29
+ def some_funky_method(args)
30
+ end
31
+ end
32
+
33
+ FactoryGirl.define do
34
+ factory :website do
35
+ more_format { "format: #{format}" }
36
+ end
37
+ end
38
+ end
39
+
40
+ after do
41
+ Object.send(:undef_method, :some_funky_method)
42
+ end
43
+
44
+ subject { FactoryGirl.build(:website, :format => "Great", :y => 12345, :some_funky_method => "foobar!") }
45
+ its(:format) { should == "Great" }
46
+ its(:y) { should == 12345 }
47
+ its(:more_format) { should == "format: Great" }
48
+ its(:some_funky_method) { should == "foobar!" }
49
+ end
50
+
51
+ describe "accessing methods from the instance within a dynamic attribute that is also a private method on object" do
52
+ before do
53
+ define_model("Website", :more_format => :string) do
54
+ def format
55
+ "This is an awesome format"
56
+ end
57
+ end
58
+
59
+ FactoryGirl.define do
60
+ factory :website do
61
+ more_format { "format: #{format}" }
62
+ end
63
+ end
64
+ end
65
+
66
+ subject { FactoryGirl.build(:website) }
67
+ its(:more_format) { should == "format: This is an awesome format" }
68
+ end
@@ -65,3 +65,26 @@ describe "an instance generated by a factory that inherits from another factory"
65
65
  end
66
66
  end
67
67
 
68
+ describe "nested factories with different parents" do
69
+ before do
70
+ define_model("User", :name => :string)
71
+
72
+ FactoryGirl.define do
73
+ factory :user do
74
+ name "Basic User"
75
+
76
+ factory :male_user do
77
+ name "John Doe"
78
+ end
79
+
80
+ factory :uppercase_male_user, :parent => :male_user do
81
+ after_build {|user| user.name = user.name.upcase }
82
+ end
83
+ end
84
+ end
85
+ end
86
+
87
+ it "honors :parent over the factory block nesting" do
88
+ FactoryGirl.build(:uppercase_male_user).name.should == "JOHN DOE"
89
+ end
90
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: factory_girl
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.5.0
4
+ version: 2.5.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-21 00:00:00.000000000Z
12
+ date: 2012-02-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &70146233062280 !ruby/object:Gem::Requirement
16
+ requirement: &70093884307140 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70146233062280
24
+ version_requirements: *70093884307140
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70146233061680 !ruby/object:Gem::Requirement
27
+ requirement: &70093884306400 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '2.0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70146233061680
35
+ version_requirements: *70093884306400
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: cucumber
38
- requirement: &70146233061160 !ruby/object:Gem::Requirement
38
+ requirement: &70093884305300 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 1.0.0
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70146233061160
46
+ version_requirements: *70093884305300
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: timecop
49
- requirement: &70146233060780 !ruby/object:Gem::Requirement
49
+ requirement: &70093884304160 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70146233060780
57
+ version_requirements: *70093884304160
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rcov
60
- requirement: &70146233060320 !ruby/object:Gem::Requirement
60
+ requirement: &70093884302020 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70146233060320
68
+ version_requirements: *70093884302020
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: aruba
71
- requirement: &70146233059900 !ruby/object:Gem::Requirement
71
+ requirement: &70093884285960 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70146233059900
79
+ version_requirements: *70093884285960
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: mocha
82
- requirement: &70146233059460 !ruby/object:Gem::Requirement
82
+ requirement: &70093884291240 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *70146233059460
90
+ version_requirements: *70093884291240
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: bourne
93
- requirement: &70146233059040 !ruby/object:Gem::Requirement
93
+ requirement: &70093884290600 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: '0'
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *70146233059040
101
+ version_requirements: *70093884290600
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: appraisal
104
- requirement: &70146233058540 !ruby/object:Gem::Requirement
104
+ requirement: &70093884288880 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ~>
@@ -109,10 +109,10 @@ dependencies:
109
109
  version: 0.3.8
110
110
  type: :development
111
111
  prerelease: false
112
- version_requirements: *70146233058540
112
+ version_requirements: *70093884288880
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: sqlite3-ruby
115
- requirement: &70146233058120 !ruby/object:Gem::Requirement
115
+ requirement: &70093884288420 !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements:
118
118
  - - ! '>='
@@ -120,10 +120,10 @@ dependencies:
120
120
  version: '0'
121
121
  type: :development
122
122
  prerelease: false
123
- version_requirements: *70146233058120
123
+ version_requirements: *70093884288420
124
124
  - !ruby/object:Gem::Dependency
125
125
  name: yard
126
- requirement: &70146233057620 !ruby/object:Gem::Requirement
126
+ requirement: &70093884287780 !ruby/object:Gem::Requirement
127
127
  none: false
128
128
  requirements:
129
129
  - - ! '>='
@@ -131,10 +131,10 @@ dependencies:
131
131
  version: '0'
132
132
  type: :development
133
133
  prerelease: false
134
- version_requirements: *70146233057620
134
+ version_requirements: *70093884287780
135
135
  - !ruby/object:Gem::Dependency
136
136
  name: bluecloth
137
- requirement: &70146233057180 !ruby/object:Gem::Requirement
137
+ requirement: &70093884286820 !ruby/object:Gem::Requirement
138
138
  none: false
139
139
  requirements:
140
140
  - - ! '>='
@@ -142,7 +142,7 @@ dependencies:
142
142
  version: '0'
143
143
  type: :development
144
144
  prerelease: false
145
- version_requirements: *70146233057180
145
+ version_requirements: *70093884286820
146
146
  description: ! "factory_girl provides a framework and DSL for defining and\n using
147
147
  factories - less error-prone, more explicit, and\n all-around
148
148
  easier to work with than fixtures."
@@ -229,7 +229,7 @@ files:
229
229
  - gemfiles/3.2.gemfile.lock
230
230
  - spec/acceptance/aliases_spec.rb
231
231
  - spec/acceptance/attribute_aliases_spec.rb
232
- - spec/acceptance/attribute_existing_on_object.rb
232
+ - spec/acceptance/attribute_existing_on_object_spec.rb
233
233
  - spec/acceptance/attributes_for_spec.rb
234
234
  - spec/acceptance/attributes_from_instance_spec.rb
235
235
  - spec/acceptance/attributes_ordered_spec.rb
@@ -310,7 +310,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
310
310
  version: '0'
311
311
  requirements: []
312
312
  rubyforge_project:
313
- rubygems_version: 1.8.10
313
+ rubygems_version: 1.8.15
314
314
  signing_key:
315
315
  specification_version: 3
316
316
  summary: factory_girl provides a framework and DSL for defining and using model instance
@@ -336,7 +336,7 @@ test_files:
336
336
  - gemfiles/3.2.gemfile.lock
337
337
  - spec/acceptance/aliases_spec.rb
338
338
  - spec/acceptance/attribute_aliases_spec.rb
339
- - spec/acceptance/attribute_existing_on_object.rb
339
+ - spec/acceptance/attribute_existing_on_object_spec.rb
340
340
  - spec/acceptance/attributes_for_spec.rb
341
341
  - spec/acceptance/attributes_from_instance_spec.rb
342
342
  - spec/acceptance/attributes_ordered_spec.rb
@@ -1,21 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe "declaring attributes on a Factory that are private methods on Object" do
4
- before do
5
- define_model("Website", :system => :boolean, :link => :string, :sleep => :integer)
6
-
7
- FactoryGirl.define do
8
- factory :website do
9
- system false
10
- link "http://example.com"
11
- sleep 15
12
- end
13
- end
14
- end
15
-
16
- subject { FactoryGirl.build(:website, :sleep => -5) }
17
-
18
- its(:system) { should == false }
19
- its(:link) { should == "http://example.com" }
20
- its(:sleep) { should == -5 }
21
- end