This repository has been archived by the owner on Nov 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory_girl_talk.rb
172 lines (91 loc) · 2.51 KB
/
factory_girl_talk.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
### GENERATORS ###
# application.rb
config.generators do |g|
g.factory_girl suffix: 'factory' # generates files like "users_factory.rb"
end
### IGNORE BLOCK & TRANSIENT ATTRIBUTES ###
factory :user do
ignore do
rockstar true #rockstar is not a real attribute on User, but we're defaulting it to true
upcased false #upcased is not a real attribute on User, but we're defaulting it to false
end
name { "John Doe#{" - Rockstar" if rockstar}" }
email { "#{name.downcase}@example.com" }
after(:create) do |user, evaluator| #the evaluator is the "context" of this factory call
user.name.upcase! if evaluator.upcased
end
end
FactoryGirl.create(:user, upcased: true).name
#=> "JOHN DOE - ROCKSTAR"
### ASSOCIATIONS & ALIASES ###
factory :post do
association :author, factory: :user # lengthy and not expressive
end
factory :post do
author #leverages the aliases on the user factory
end
factory :user, aliases: [:author, :commenter] do
#...
end
### TRAITS ###
factory :post do
title "A title"
factory :approved_post do
approved true
factory :approved_post_with_author do
author
end
end
factory :unapproved_post do
approved false
factory :unapproved_post_with_author do
author
end
end
end
# Factory inheritance makes Will a sad panda :(
# Composition via traits!
factory :post do
title "A title"
trait :approved do
approved true
end
trait :unapproved do
approved false
end
trait :with_author do
author
end
factory :approved_post, traits: [:approved]
factory :approved_post_with_author, traits: [:approved, :with_author]
factory :unapproved_post, traits: [:unapproved]
factory :unapproved_post_with_author, traits: [:unapproved, :with_author]
end
FactoryGirl.create(:approved_post_with_author, title: "Best Post")
FactoryGirl.create(:post, :approved, title: "Best Post")
FactoryGirl.create(:post, :approved, :with_author, title: "Best Post")
### MODIFY FACTORIES IN GEMS ###
# from gem...
FactoryGirl.define do
factory :user do
full_name "John Doe"
sequence(:username) { |n| "user#{n}" }
password "password"
end
end
# in your project that uses the gem...
FactoryGirl.modify do
factory :user do
full_name "Jane Doe"
health 90
end
end
### BILLION DOLLAR IDEA? DROP THE "THE" ###
# spec/support/factory_girl.rb
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
FactoryGirl.create(:user)
create(:user)
FactoryGirl.build(:user)
build(:user)