diff --git a/Gemfile b/Gemfile index 29fcdeef..68460d1a 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/Gemfile.lock b/Gemfile.lock index 45f1595d..bdec2ffd 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) diff --git a/config/application.rb b/config/application.rb index 6ea74494..c6e90abb 100644 --- a/config/application.rb +++ b/config/application.rb @@ -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 diff --git a/spec/factories/notes.rb b/spec/factories/notes.rb new file mode 100644 index 00000000..06d32ae4 --- /dev/null +++ b/spec/factories/notes.rb @@ -0,0 +1,7 @@ +FactoryBot.define do + factory :note do + message "My important note." + association :project + user { project.owner } + end +end diff --git a/spec/factories/projects.rb b/spec/factories/projects.rb new file mode 100644 index 00000000..aa7ce3c6 --- /dev/null +++ b/spec/factories/projects.rb @@ -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 diff --git a/spec/factories/users.rb b/spec/factories/users.rb new file mode 100644 index 00000000..ff172d27 --- /dev/null +++ b/spec/factories/users.rb @@ -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 diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index 13b610ee..8adf092f 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -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 diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 02097563..4ce9e26e 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -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", @@ -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: "tester@example.com", - password: "dottle-nouveau-pavilion-tights-furze", - ) - user = User.new( - first_name: "Jane", - last_name: "Tester", - email: "tester@example.com", - password: "dottle-nouveau-pavilion-tights-furze", - ) + FactoryBot.create(:user, email: "aaron@example.com") + user = FactoryBot.build(:user, email: "aaron@example.com") 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: "johndoe@example.com", - ) + user = FactoryBot.build(:user, first_name: "John", last_name: "Doe") expect(user.name).to eq "John Doe" end end