Skip to content
This repository has been archived by the owner on Jul 21, 2024. It is now read-only.

04 factories #30

Open
wants to merge 31 commits into
base: 03-models
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
6091092
Install dependency on factory_girl_rails
ruralocity May 22, 2017
8bda8b8
Add initial factory examples for chapter 4
ruralocity May 24, 2017
9b24979
Merge branch '03-models' into 04-factories
ruralocity May 24, 2017
48b7a53
Add factory inheritance example for chapter 4
ruralocity May 24, 2017
c2e9f9f
Add factory inheritance examples for chapter 4
ruralocity May 24, 2017
034bfb9
Add factory trait examples for chapter 4
ruralocity May 24, 2017
1bc38f6
Add factory callback example for chapter 4
ruralocity May 24, 2017
2e268ff
Merge branch '03-models' into 04-factories
ruralocity May 25, 2017
2da89e7
Merge branch '03-models' into 04-factories
ruralocity May 27, 2017
26b4223
Merge branch '03-models' into 04-factories
ruralocity May 27, 2017
20fd942
Merge branch '03-models' into 04-factories
ruralocity May 28, 2017
09c6769
Merge branch '03-models' into 04-factories
ruralocity May 31, 2017
381985b
Merge branch '03-models' into 04-factories
ruralocity Jun 7, 2017
d2a13f8
Merge branch '03-models' into 04-factories
ruralocity Jun 10, 2017
def44e2
Merge branch '03-models' into 04-factories
ruralocity Jun 19, 2017
ef68377
Merge branch '02-setup' into 03-models
ruralocity Jun 23, 2017
aeb244d
Merge branch '03-models' into 04-factories
ruralocity Jun 23, 2017
c47775b
Merge branch '03-models' into 04-factories
ruralocity Jun 30, 2017
db32241
Merge branch '03-models' into 04-factories
ruralocity Jul 11, 2017
04c43e6
Merge branch '03-models' into 04-factories
ruralocity Jul 11, 2017
168f8e3
Merge branch '03-models' into 04-factories
ruralocity Aug 5, 2017
2d78280
Merge branch '03-models' into 04-factories
ruralocity Aug 10, 2017
52a606f
Include time zone in projects factory
ruralocity Aug 10, 2017
97b76d0
Merge branch '03-models' into 04-factories
ruralocity Aug 30, 2017
667bea9
Merge branch '03-models' into 04-factories
ruralocity Aug 30, 2017
a214efb
Merge branch '03-models' into 04-factories
ruralocity Sep 16, 2017
03278be
Merge branch '03-models' into 04-factories
ruralocity Oct 18, 2017
7940227
Replace FactoryGirl with FactoryBot
ruralocity Jun 5, 2018
1eb0142
Merge branch '03-models' into 04-factories
ruralocity Aug 1, 2018
0b1abb2
Merge branch '03-models' into 04-factories
ruralocity Aug 9, 2018
967dc26
Merge branch '03-models' into 04-factories
ruralocity Aug 9, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ gem 'jbuilder', '~> 2.5'

group :development, :test do
gem 'rspec-rails', '~> 3.6.0'
gem 'factory_bot_rails', '~> 4.10.0'
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
gem 'capybara', '~> 2.13.0'
gem 'selenium-webdriver'
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ GEM
diff-lcs (1.3)
erubi (1.7.1)
execjs (2.7.0)
factory_bot (4.10.0)
activesupport (>= 3.0.0)
factory_bot_rails (4.10.0)
factory_bot (~> 4.10.0)
railties (>= 3.0.0)
faker (1.7.3)
i18n (~> 0.5)
ffi (1.9.18)
Expand Down Expand Up @@ -235,6 +240,7 @@ DEPENDENCIES
capybara (~> 2.13.0)
coffee-rails (~> 4.2)
devise
factory_bot_rails (~> 4.10.0)
faker
geocoder
jbuilder (~> 2.5)
Expand Down
1 change: 0 additions & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class Application < Rails::Application

config.generators do |g|
g.test_framework :rspec,
fixtures: false,
view_specs: false,
helper_specs: false,
routing_specs: false
Expand Down
7 changes: 7 additions & 0 deletions spec/factories/notes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FactoryBot.define do
factory :note do
message "My important note."
association :project
user { project.owner }
end
end
61 changes: 61 additions & 0 deletions spec/factories/projects.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
FactoryBot.define do
factory :project do
sequence(:name) { |n| "Project #{n}" }
description "A test project."
due_on 1.week.from_now
association :owner

trait :with_notes do
after(:create) { |project| create_list(:note, 5, project: project) }
end

trait :due_yesterday do
due_on 1.day.ago
end

trait :due_today do
due_on Date.current.in_time_zone
end

trait :due_tomorrow do
due_on 1.day.from_now
end

# Factory inheritance examples ...
#
# factory :project_due_yesterday do
# due_on 1.day.ago
# end
#
# factory :project_due_today do
# due_on Date.current.in_time_zone
# end
#
# factory :project_due_tomorrow do
# due_on 1.day.from_now
# end
end

# Non-DRY versions ...
#
# factory :project_due_yesterday, class: Project do
# sequence(:name) { |n| "Test Project #{n}" }
# description "Sample project for testing purposes"
# due_on 1.day.ago
# association :owner
# end
#
# factory :project_due_today, class: Project do
# sequence(:name) { |n| "Test Project #{n}" }
# description "Sample project for testing purposes"
# due_on Date.today
# association :owner
# end
#
# factory :project_due_tomorrow, class: Project do
# sequence(:name) { |n| "Test Project #{n}" }
# description "Sample project for testing purposes"
# due_on 1.day.from_now
# association :owner
# end
end
8 changes: 8 additions & 0 deletions spec/factories/users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FactoryBot.define do
factory :user, aliases: [:owner] do
first_name "Aaron"
last_name "Sumner"
sequence(:email) { |n| "tester#{n}@example.com" }
password "dottle-nouveau-pavilion-tights-furze"
end
end
22 changes: 22 additions & 0 deletions spec/models/project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,26 @@

expect(other_project).to be_valid
end

describe "late status" do
it "is late when the due date is past today" do
project = FactoryBot.create(:project, :due_yesterday)
expect(project).to be_late
end

it "is on time when the due date is today" do
project = FactoryBot.create(:project, :due_today)
expect(project).to_not be_late
end

it "is on time when the due date is in the future" do
project = FactoryBot.create(:project, :due_tomorrow)
expect(project).to_not be_late
end
end

it "can have many notes" do
project = FactoryBot.create(:project, :with_notes)
expect(project.notes.length).to eq 5
end
end
30 changes: 10 additions & 20 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
require 'rails_helper'

RSpec.describe User, type: :model do
it "has a valid factory" do
expect(FactoryBot.build(:user)).to be_valid
end

it "is valid with a first name, last name and email, and password" do
user = User.new(
first_name: "Aaron",
Expand All @@ -12,46 +16,32 @@
end

it "is invalid without a first name" do
user = User.new(first_name: nil)
user = FactoryBot.build(:user, first_name: nil)
user.valid?
expect(user.errors[:first_name]).to include("can't be blank")
end

it "is invalid without a last name" do
user = User.new(last_name: nil)
user = FactoryBot.build(:user, last_name: nil)
user.valid?
expect(user.errors[:last_name]).to include("can't be blank")
end

it "is invalid without an email address" do
user = User.new(email: nil)
user = FactoryBot.build(:user, email: nil)
user.valid?
expect(user.errors[:email]).to include("can't be blank")
end

it "is invalid with a duplicate email address" do
User.create(
first_name: "Joe",
last_name: "Tester",
email: "[email protected]",
password: "dottle-nouveau-pavilion-tights-furze",
)
user = User.new(
first_name: "Jane",
last_name: "Tester",
email: "[email protected]",
password: "dottle-nouveau-pavilion-tights-furze",
)
FactoryBot.create(:user, email: "[email protected]")
user = FactoryBot.build(:user, email: "[email protected]")
user.valid?
expect(user.errors[:email]).to include("has already been taken")
end

it "returns a user's full name as a string" do
user = User.new(
first_name: "John",
last_name: "Doe",
email: "[email protected]",
)
user = FactoryBot.build(:user, first_name: "John", last_name: "Doe")
expect(user.name).to eq "John Doe"
end
end