# BAD
it "#full_name concatenates first and last name" do
user = create(
:user,
first_name: "Joël",
last_name: "Quenneville"
)
expect(user.full_name).to eq "Joël Quenneville"
end
Slide 9
Slide 9 text
# GOOD
it "#full_name concatenates first and last name" do
user = User.new(
first_name: "Joël",
last_name: "Quenneville"
)
expect(user.full_name).to eq "Joël Quenneville"
end
Slide 10
Slide 10 text
No content
Slide 11
Slide 11 text
No content
Slide 12
Slide 12 text
No content
Slide 13
Slide 13 text
No content
Slide 14
Slide 14 text
No content
Slide 15
Slide 15 text
No content
Slide 16
Slide 16 text
No content
Slide 17
Slide 17 text
No content
Slide 18
Slide 18 text
No content
Slide 19
Slide 19 text
let(:user1) { create(:user) }
let(:user2) { create(:user) }
let(:organization) { create(:organization), users: [user1, user2] }
# this test only needs an org
it "does something"
# this test needs and org + some users
it "does something whith multiple users"
Slide 20
Slide 20 text
No content
Slide 21
Slide 21 text
No content
Slide 22
Slide 22 text
No content
Slide 23
Slide 23 text
No content
Slide 24
Slide 24 text
No content
Slide 25
Slide 25 text
let(:contact) { create(:contact) }
let(:user) { create(:user, admin: true, contacts: [contact]) }
# NEEDS 1 query
# MAKES 3 queries (2x CREATE + 1x UPDATE)
it "does something for a regular user" do
user.update(admin: false)
...
end
# NEEDS 2 queries
# MAKES 2 queries
it "does something for an admin with contact info"
Slide 26
Slide 26 text
No content
Slide 27
Slide 27 text
# this test only needs an org
it "does something" do
org = create(:organization)
...
end
# this test needs and org + some users
it "does something whith multiple users" do
user1 = create(:user)
user2 = create(:user)
org = create(:organization, users: [user1, user2]
...
end
Slide 28
Slide 28 text
No content
Slide 29
Slide 29 text
No content
Slide 30
Slide 30 text
No content
Slide 31
Slide 31 text
No content
Slide 32
Slide 32 text
No content
Slide 33
Slide 33 text
No content
Slide 34
Slide 34 text
factory :organization do
members { create_list(:user, 3 }
sequence(:name) { |n| "Organization #{n}" }
end
Slide 35
Slide 35 text
it "does something" do
# this makes 4 INSERT queries
org = create(:organization)
expect(org.do_something).to eq "something"
end
Slide 36
Slide 36 text
No content
Slide 37
Slide 37 text
factory :organization do
members { create_list(:user, 3 }
sequence(:name) { |n| "Organization #{n}" }
end
factory :user do
association :organization
end
Slide 38
Slide 38 text
it "does something given a user" do
user = create(:user)
service = MyService.new(user)
expect(service.call()).to eq "something"
end
Slide 39
Slide 39 text
No content
Slide 40
Slide 40 text
No content
Slide 41
Slide 41 text
factory :organization do
sequence(:name) { |n| "Organization #{n}" }
end
Slide 42
Slide 42 text
factory :organization do
sequence(:name) { |n| "Organization #{n}" }
trait :with_users do
members { create_list(:user, 3 }
end
end
[TEST PROF INFO] Factories usage
Total: 15285
Total top-level: 10286
Total time: 04:31.222 (out of 07.16.124)
Total uniq factories: 119
total top-level total time time per call top-level time name
6091 2715 115.7671s 0.0426s 50.2517s user
2142 2098 93.3152s 0.0444s 92.1915s post
...
Slide 46
Slide 46 text
No content
Slide 47
Slide 47 text
No content
Slide 48
Slide 48 text
class Organization < ApplicationRecord
before_create :generate_admin
def generate_admin
admin = User.create!(
email: "admin@#{organization.domain}",
admin: true
)
self.admin = admin
end
end
Slide 49
Slide 49 text
it "does something" do
admin = create(:user, admin: true)
org = create(:organization, admin: admin)
# ...
end
Slide 50
Slide 50 text
No content
Slide 51
Slide 51 text
No content
Slide 52
Slide 52 text
it "does something" do
admin = create(:user, admin: true)
org = create(:organization)
# CODE SMELL!
# This now makes 3 INSERTs + 1 UPDATE calls
org.update(admin: admin)
# ...
end
Slide 53
Slide 53 text
No content
Slide 54
Slide 54 text
No content
Slide 55
Slide 55 text
No content
Slide 56
Slide 56 text
factory_bot_results = {}
config.before(:suite) do
ActiveSupport::Notifications.subscribe("factory_bot.run_factory") do |name, start,
finish, id, payload|
# aggregate results
end
config.after(:suite) do
puts factory_bot_results
end
Slide 57
Slide 57 text
No content
Slide 58
Slide 58 text
No content
Slide 59
Slide 59 text
No content
Slide 60
Slide 60 text
No content
Slide 61
Slide 61 text
factory :organization_with_events do
staff { create_list(:staff, 1) }
venues { create_list(:venue, 1)
after(:build) do |org|
create_list(:event, 3, venue: org.venues.first)
end
end
Slide 62
Slide 62 text
factory :organization_with_events do
staff { create_list(:staff, 1) }
venues { create_list(:venue, 1)
after(:build) do |org|
create_list(:event, 3, venue: org.venues.first, staff: org.staff.first)
end
end