factory_girl 2.4.1 → 2.4.2

Sign up to get free protection for your applications and to get access to all the features.
data/Changelog CHANGED
@@ -1,3 +1,6 @@
1
+ 2.4.2 (January 18, 2012)
2
+ Fix inline traits' interaction with defaults on the factory
3
+
1
4
  2.4.1 (January 17, 2012)
2
5
  Deprecate build_stubbed and attributes_for
3
6
  Fix inline traits
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- factory_girl (2.4.1)
4
+ factory_girl (2.4.2)
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.4.1)
4
+ factory_girl (2.4.2)
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.4.1)
4
+ factory_girl (2.4.2)
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.4.1)
4
+ factory_girl (2.4.2)
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.4.1)
4
+ factory_girl (2.4.2)
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.4.1)
4
+ factory_girl (2.4.2)
5
5
  activesupport
6
6
 
7
7
  GEM
@@ -2,12 +2,13 @@ module FactoryGirl
2
2
  class Definition
3
3
  attr_reader :callbacks, :defined_traits, :declarations
4
4
 
5
- def initialize(name = nil)
6
- @declarations = DeclarationList.new(name)
7
- @callbacks = []
8
- @defined_traits = []
9
- @to_create = lambda {|instance| instance.save! }
10
- @traits = []
5
+ def initialize(name = nil, base_traits = [])
6
+ @declarations = DeclarationList.new(name)
7
+ @callbacks = []
8
+ @defined_traits = []
9
+ @to_create = lambda {|instance| instance.save! }
10
+ @base_traits = base_traits
11
+ @additional_traits = []
11
12
  end
12
13
 
13
14
  delegate :declare_attribute, :to => :declarations
@@ -20,17 +21,17 @@ module FactoryGirl
20
21
  attributes
21
22
  end
22
23
 
24
+ def processing_order
25
+ base_traits + [self] + additional_traits
26
+ end
27
+
23
28
  def overridable
24
29
  declarations.overridable
25
30
  self
26
31
  end
27
32
 
28
- def traits
29
- @traits.reverse.map { |name| trait_by_name(name) }
30
- end
31
-
32
33
  def inherit_traits(new_traits)
33
- @traits += new_traits
34
+ @additional_traits += new_traits
34
35
  end
35
36
 
36
37
  def add_callback(callback)
@@ -51,6 +52,14 @@ module FactoryGirl
51
52
 
52
53
  private
53
54
 
55
+ def base_traits
56
+ @base_traits.map { |name| trait_by_name(name) }
57
+ end
58
+
59
+ def additional_traits
60
+ @additional_traits.map { |name| trait_by_name(name) }
61
+ end
62
+
54
63
  def trait_by_name(name)
55
64
  trait_for(name) || FactoryGirl.trait_by_name(name)
56
65
  end
@@ -12,14 +12,12 @@ module FactoryGirl
12
12
  @aliases = options[:aliases] || []
13
13
  @class_name = options[:class]
14
14
  @default_strategy = options[:default_strategy]
15
- @definition = Definition.new(@name)
15
+ @definition = Definition.new(@name, options[:traits] || [])
16
16
  @compiled = false
17
-
18
- inherit_traits(options[:traits] || [])
19
17
  end
20
18
 
21
19
  delegate :add_callback, :declare_attribute, :to_create, :define_trait,
22
- :defined_traits, :traits, :inherit_traits, :to => :@definition
20
+ :defined_traits, :inherit_traits, :processing_order, :to => :@definition
23
21
 
24
22
  def factory_name
25
23
  $stderr.puts "DEPRECATION WARNING: factory.factory_name is deprecated; use factory.name instead."
@@ -127,10 +125,6 @@ module FactoryGirl
127
125
 
128
126
  private
129
127
 
130
- def processing_order
131
- [traits.reverse, @definition].flatten
132
- end
133
-
134
128
  def assert_valid_options(options)
135
129
  options.assert_valid_keys(:class, :parent, :default_strategy, :aliases, :traits)
136
130
 
@@ -1,4 +1,4 @@
1
1
  module FactoryGirl
2
- VERSION = "2.4.1"
2
+ VERSION = "2.4.2"
3
3
  end
4
4
 
@@ -312,3 +312,51 @@ describe "applying inline traits" do
312
312
  FactoryGirl.create(:user, :with_post).posts.should_not be_empty
313
313
  end
314
314
  end
315
+
316
+ describe "inline traits overriding existing attributes" do
317
+ before do
318
+ define_model("User", :status => :string)
319
+
320
+ FactoryGirl.define do
321
+ factory :user do
322
+ status "pending"
323
+
324
+ trait(:accepted) { status "accepted" }
325
+ trait(:declined) { status "declined" }
326
+
327
+ factory :declined_user, :traits => [:declined]
328
+ factory :extended_declined_user, :traits => [:declined] do
329
+ status "extended_declined"
330
+ end
331
+ end
332
+ end
333
+ end
334
+
335
+ it "returns the default status" do
336
+ FactoryGirl.build(:user).status.should == "pending"
337
+ end
338
+
339
+ it "prefers inline trait attributes over default attributes" do
340
+ FactoryGirl.build(:user, :accepted).status.should == "accepted"
341
+ end
342
+
343
+ it "prefers traits on a factory over default attributes" do
344
+ FactoryGirl.build(:declined_user).status.should == "declined"
345
+ end
346
+
347
+ it "prefers inline trait attributes over traits on a factory" do
348
+ FactoryGirl.build(:declined_user, :accepted).status.should == "accepted"
349
+ end
350
+
351
+ it "prefers attributes on factories over attributes from non-inline traits" do
352
+ FactoryGirl.build(:extended_declined_user).status.should == "extended_declined"
353
+ end
354
+
355
+ it "prefers inline traits over attributes on factories" do
356
+ FactoryGirl.build(:extended_declined_user, :accepted).status.should == "accepted"
357
+ end
358
+
359
+ it "prefers overridden attributes over attributes from traits, inline traits, or attributes on factories" do
360
+ FactoryGirl.build(:extended_declined_user, :accepted, :status => "completely overridden").status.should == "completely overridden"
361
+ end
362
+ end
@@ -64,24 +64,41 @@ describe FactoryGirl::Definition, "#to_create" do
64
64
  end
65
65
  end
66
66
 
67
- describe FactoryGirl::Definition, "#traits" do
68
- let(:female_trait) { stub("female trait", :name => :female) }
69
- let(:admin_trait) { stub("admin trait", :name => :admin) }
67
+ describe FactoryGirl::Definition, "#processing_order" do
68
+ let(:female_trait) { FactoryGirl::Trait.new(:female) }
69
+ let(:admin_trait) { FactoryGirl::Trait.new(:admin) }
70
70
 
71
71
  before do
72
72
  subject.define_trait(female_trait)
73
73
  FactoryGirl.stubs(:trait_by_name => admin_trait)
74
74
  end
75
75
 
76
- its(:traits) { should be_empty }
76
+ context "without base traits" do
77
+ it "returns the definition without any traits" do
78
+ subject.processing_order.should == [subject]
79
+ end
77
80
 
78
- it "finds the correct traits after inheriting" do
79
- subject.inherit_traits([:female])
80
- subject.traits.should == [female_trait]
81
+ it "finds the correct traits after inheriting" do
82
+ subject.inherit_traits([:female])
83
+ subject.processing_order.should == [subject, female_trait]
84
+ end
85
+
86
+ it "looks for the trait on FactoryGirl" do
87
+ subject.inherit_traits([:female, :admin])
88
+ subject.processing_order.should == [subject, female_trait, admin_trait]
89
+ end
81
90
  end
82
91
 
83
- it "looks for the trait on FactoryGirl" do
84
- subject.inherit_traits([:female, :admin])
85
- subject.traits.should == [admin_trait, female_trait]
92
+ context "with base traits" do
93
+ subject { FactoryGirl::Definition.new("my definition", [:female]) }
94
+
95
+ it "returns the base traits and definition" do
96
+ subject.processing_order.should == [female_trait, subject]
97
+ end
98
+
99
+ it "finds the correct traits after inheriting" do
100
+ subject.inherit_traits([:admin])
101
+ subject.processing_order.should == [female_trait, subject, admin_trait]
102
+ end
86
103
  end
87
104
  end
@@ -305,11 +305,11 @@ describe FactoryGirl::Factory, "#with_traits" do
305
305
  end
306
306
 
307
307
  it "returns a factory with the correct traits" do
308
- subject.with_traits([:admin, :female]).traits.should =~ [admin_trait, female_trait]
308
+ subject.with_traits([:admin, :female]).processing_order[1, 2].should == [admin_trait, female_trait]
309
309
  end
310
310
 
311
- it "doesn't modify the original factory's traits" do
311
+ it "doesn't modify the original factory's processing order" do
312
312
  subject.with_traits([:admin, :female])
313
- subject.traits.should be_empty
313
+ subject.processing_order.should == [subject.definition]
314
314
  end
315
315
  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.4.1
4
+ version: 2.4.2
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-17 00:00:00.000000000Z
12
+ date: 2012-01-18 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &70308695491200 !ruby/object:Gem::Requirement
16
+ requirement: &70205633314300 !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: *70308695491200
24
+ version_requirements: *70205633314300
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70308695490500 !ruby/object:Gem::Requirement
27
+ requirement: &70205633310180 !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: *70308695490500
35
+ version_requirements: *70205633310180
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: cucumber
38
- requirement: &70308695489400 !ruby/object:Gem::Requirement
38
+ requirement: &70205633309440 !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: *70308695489400
46
+ version_requirements: *70205633309440
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: timecop
49
- requirement: &70308695489000 !ruby/object:Gem::Requirement
49
+ requirement: &70205633308320 !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: *70308695489000
57
+ version_requirements: *70205633308320
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rcov
60
- requirement: &70308695488540 !ruby/object:Gem::Requirement
60
+ requirement: &70205633307580 !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: *70308695488540
68
+ version_requirements: *70205633307580
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: aruba
71
- requirement: &70308695488000 !ruby/object:Gem::Requirement
71
+ requirement: &70205633306600 !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: *70308695488000
79
+ version_requirements: *70205633306600
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: mocha
82
- requirement: &70308695487580 !ruby/object:Gem::Requirement
82
+ requirement: &70205633305800 !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: *70308695487580
90
+ version_requirements: *70205633305800
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: bourne
93
- requirement: &70308695481180 !ruby/object:Gem::Requirement
93
+ requirement: &70205633305240 !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: *70308695481180
101
+ version_requirements: *70205633305240
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: appraisal
104
- requirement: &70308695480520 !ruby/object:Gem::Requirement
104
+ requirement: &70205633304740 !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: *70308695480520
112
+ version_requirements: *70205633304740
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: sqlite3-ruby
115
- requirement: &70308695479860 !ruby/object:Gem::Requirement
115
+ requirement: &70205633304300 !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: *70308695479860
123
+ version_requirements: *70205633304300
124
124
  - !ruby/object:Gem::Dependency
125
125
  name: yard
126
- requirement: &70308695479120 !ruby/object:Gem::Requirement
126
+ requirement: &70205633303840 !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: *70308695479120
134
+ version_requirements: *70205633303840
135
135
  - !ruby/object:Gem::Dependency
136
136
  name: bluecloth
137
- requirement: &70308695478620 !ruby/object:Gem::Requirement
137
+ requirement: &70205633303340 !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: *70308695478620
145
+ version_requirements: *70205633303340
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."