/
factory_girl.rb
64 lines (49 loc) · 1.52 KB
/
factory_girl.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
# https://github.com/thoughtbot/factory_girl
# http://rubydoc.info/gems/factory_girl/1.3.3/frames
### DEFINE FACTORIES in test/factories/*.rb:
# This will guess the User class
Factory.define :user do |u|
u.first_name 'John'
u.last_name 'Doe'
u.admin false
end
# This will use the User class (Admin would have been guessed)
Factory.define :admin, :class => User do |u|
u.first_name 'Admin'
u.last_name 'User'
u.admin true
end
# Dependent attributes (Set an attribute using values of other attributes)
Factory.define :user do |u|
u.first_name 'Joe'
u.last_name 'Blow'
u.email {|a| "#{a.first_name}.#{a.last_name}@example.com".downcase }
end
# Associations
Factory.define :post do |p|
p.author {|author| author.association(:user, :last_name => 'Writely') }
# OR:
p.association :author, :factory => :user
end
# Inheritance
Factory.define :approved_post, :parent => :post do |p|
p.approved true
# the 'approver' association is required for an approved post
p.association :approver, :factory => :user
end
# Callbacks
Factory.define :user do |u|
u.after_build { |user| do_something_to(user) }
u.after_create { |user| do_something_else_to(user) }
end
### USE FACTORIES in test/unit/*.rb
# Same as Factory.create :user:
user = Factory(:user)
# Returns a User instance that's not saved
user = Factory.build(:user)
# Returns a saved User instance
user = Factory.create(:user)
# Build a User instance and override the first_name property
user = Factory.build(:user, :first_name => 'Joe')
user.first_name
# => "Joe"