From a590b89302dd65df53cec38d42b14cbe63cc2dd3 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Mon, 4 Jun 2018 12:21:19 +1200 Subject: [PATCH] Photos in spec needs to have same owner as item they link to --- app/controllers/notifications_controller.rb | 2 +- .../photo_associations_controller.rb | 2 + app/models/photographing.rb | 2 +- app/models/planting.rb | 2 +- .../notifications_controller_spec.rb | 2 +- .../photo_associations_controller_spec.rb | 8 ++- spec/features/home/home_spec.rb | 4 +- spec/features/photos/show_photo_spec.rb | 6 +- spec/helpers/photos_helper_spec.rb | 15 ++-- spec/models/crop_spec.rb | 57 +++++++-------- spec/models/garden_spec.rb | 34 ++++----- spec/models/harvest_spec.rb | 49 +++++++------ spec/models/photo_spec.rb | 6 +- spec/models/planting_spec.rb | 71 ++++++++----------- spec/models/seed_spec.rb | 4 +- spec/views/crops/index.html.haml_spec.rb | 2 +- 16 files changed, 133 insertions(+), 133 deletions(-) diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb index bbab48b43..7c3c1f322 100644 --- a/app/controllers/notifications_controller.rb +++ b/app/controllers/notifications_controller.rb @@ -20,7 +20,7 @@ class NotificationsController < ApplicationController def new @notification = Notification.new - @recipient = Member.find_by(id: params[:recipient]) + @recipient = Member.find_by(id: params[:recipient_id]) @subject = params[:subject] || "" end diff --git a/app/controllers/photo_associations_controller.rb b/app/controllers/photo_associations_controller.rb index 126aaf9b3..c63cca398 100644 --- a/app/controllers/photo_associations_controller.rb +++ b/app/controllers/photo_associations_controller.rb @@ -11,6 +11,8 @@ class PhotoAssociationsController < ApplicationController respond_with(@photo) end + private + def item_class params[:type].capitalize end diff --git a/app/models/photographing.rb b/app/models/photographing.rb index e8999db43..040817611 100644 --- a/app/models/photographing.rb +++ b/app/models/photographing.rb @@ -15,6 +15,6 @@ class Photographing < ApplicationRecord private def photo_and_item_have_same_owner - errors.add(:photo, "must have same owner as item it links to") unless photographable.owner == photo.owner + errors.add(:photo, "must have same owner as item it links to") unless photographable.owner_id == photo.owner_id end end diff --git a/app/models/planting.rb b/app/models/planting.rb index 144d3ee42..f6966d94f 100644 --- a/app/models/planting.rb +++ b/app/models/planting.rb @@ -29,7 +29,7 @@ class Planting < ApplicationRecord ## ## Scopes default_scope { joins(:owner) } # Ensures the owner still exists - scope :interesting, -> { has_photos.one_per_owner } + scope :interesting, -> { has_photos.one_per_owner.order(planted_at: :desc) } scope :recent, -> { order(created_at: :desc) } scope :one_per_owner, lambda { joins("JOIN members m ON (m.id=plantings.owner_id) diff --git a/spec/controllers/notifications_controller_spec.rb b/spec/controllers/notifications_controller_spec.rb index a7ed81ae5..30cf37ddd 100644 --- a/spec/controllers/notifications_controller_spec.rb +++ b/spec/controllers/notifications_controller_spec.rb @@ -76,7 +76,7 @@ describe NotificationsController do it "assigns a recipient" do @recipient = FactoryBot.create(:member) get :new, params: { recipient_id: @recipient.id } - assigns(:recipient).should be_an_instance_of(Member) + expect(assigns(:recipient)).to be_an_instance_of(Member) end end diff --git a/spec/controllers/photo_associations_controller_spec.rb b/spec/controllers/photo_associations_controller_spec.rb index bbac961fa..ffde9e1e3 100644 --- a/spec/controllers/photo_associations_controller_spec.rb +++ b/spec/controllers/photo_associations_controller_spec.rb @@ -24,7 +24,7 @@ describe PhotoAssociationsController do end describe "another member's harvest from another member's photo" do - let(:harvest) { FactoryBot.create :harvest } + let(:harvest) { FactoryBot.create :harvest, owner: photo.owner } let(:photo) { FactoryBot.create :photo } it do @@ -36,7 +36,11 @@ describe PhotoAssociationsController do end end.not_to change(photo.harvests, :count) end - it { expect { delete :destroy, params: valid_params }.to raise_error(ActiveRecord::RecordNotFound) } + + it do + delete :destroy, params: valid_params + expect(response).to have_http_response(:not_found) + end end end end diff --git a/spec/features/home/home_spec.rb b/spec/features/home/home_spec.rb index 4137b5e5a..ebbcb9fcb 100644 --- a/spec/features/home/home_spec.rb +++ b/spec/features/home/home_spec.rb @@ -2,8 +2,8 @@ require 'rails_helper' feature "home page" do let(:member) { FactoryBot.create :member } - # let(:seed_photo) { FactoryBot.create :photo } - let(:photo) { FactoryBot.create :photo } + + let(:photo) { FactoryBot.create :photo, owner: member } let(:crop) { FactoryBot.create :crop, created_at: 1.day.ago } let(:planting) { FactoryBot.create :planting, owner: member, crop: crop } diff --git a/spec/features/photos/show_photo_spec.rb b/spec/features/photos/show_photo_spec.rb index bac25b370..2fa9d7119 100644 --- a/spec/features/photos/show_photo_spec.rb +++ b/spec/features/photos/show_photo_spec.rb @@ -1,8 +1,6 @@ require 'rails_helper' feature "show photo page" do - let(:photo) { create :photo } - context "signed in member" do let(:member) { create :member } @@ -10,6 +8,7 @@ feature "show photo page" do context "linked to planting" do let(:planting) { create :planting } + let(:photo) { create :photo, owner: planting.owner } scenario "shows linkback to planting" do planting.photos << photo @@ -20,6 +19,7 @@ feature "show photo page" do end context "linked to harvest" do + let(:photo) { create :photo, owner: harvest.owner } let(:harvest) { create :harvest } scenario "shows linkback to harvest" do @@ -30,6 +30,7 @@ feature "show photo page" do end context "linked to garden" do + let(:photo) { create :photo, owner: garden.owner } let(:garden) { create :garden } scenario "shows linkback to garden" do @@ -40,6 +41,7 @@ feature "show photo page" do end context "linked to seed" do + let(:photo) { create :photo, owner: seed.owner } let(:seed) { create :seed } scenario "shows linkback to seed" do diff --git a/spec/helpers/photos_helper_spec.rb b/spec/helpers/photos_helper_spec.rb index 589cf8104..6a17ae4fd 100644 --- a/spec/helpers/photos_helper_spec.rb +++ b/spec/helpers/photos_helper_spec.rb @@ -4,13 +4,14 @@ describe PhotosHelper do let(:crop) { FactoryBot.create :crop } let(:garden) { FactoryBot.create :garden } - let(:garden_photo) { FactoryBot.create(:photo, thumbnail_url: 'garden.jpg') } - let(:planting) { FactoryBot.create :planting, crop: crop } - let(:planting_photo) { FactoryBot.create(:photo, thumbnail_url: 'planting.jpg') } - let(:harvest) { FactoryBot.create :harvest, crop: crop } - let(:harvest_photo) { FactoryBot.create(:photo, thumbnail_url: 'harvest.jpg') } - let(:seed) { FactoryBot.create :seed, crop: crop } - let(:seed_photo) { FactoryBot.create(:photo, thumbnail_url: 'seed.jpg') } + + let(:garden_photo) { FactoryBot.create(:photo, thumbnail_url: 'garden.jpg', owner: garden.owner) } + let(:planting) { FactoryBot.create :planting, crop: crop, owner: garden.owner } + let(:planting_photo) { FactoryBot.create(:photo, thumbnail_url: 'planting.jpg', owner: garden.owner) } + let(:harvest) { FactoryBot.create :harvest, crop: crop, owner: garden.owner } + let(:harvest_photo) { FactoryBot.create(:photo, thumbnail_url: 'harvest.jpg', owner: garden.owner) } + let(:seed) { FactoryBot.create :seed, crop: crop, owner: garden.owner } + let(:seed_photo) { FactoryBot.create(:photo, thumbnail_url: 'seed.jpg', owner: garden.owner) } describe "crops" do subject { crop_image_path(crop) } diff --git a/spec/models/crop_spec.rb b/spec/models/crop_spec.rb index 3f49a249d..415cf922a 100644 --- a/spec/models/crop_spec.rb +++ b/spec/models/crop_spec.rb @@ -55,26 +55,26 @@ describe Crop do end it "sorts by most plantings" do - Crop.popular.first.should eq maize + expect(Crop.popular.first).to eq maize FactoryBot.create_list(:planting, 10, crop: tomato) - Crop.popular.first.should eq tomato + expect(Crop.popular.first).to eq tomato end end it 'finds a default scientific name' do @crop = FactoryBot.create(:tomato) - @crop.default_scientific_name.should eq nil + expect(@crop.default_scientific_name).to eq nil @sn = FactoryBot.create(:solanum_lycopersicum, crop: @crop) @crop.reload - @crop.default_scientific_name.should eq @sn.name + expect(@crop.default_scientific_name).to eq @sn.name end it 'counts plantings' do @crop = FactoryBot.create(:tomato) - @crop.plantings.size.should eq 0 + expect(@crop.plantings.size).to eq 0 @planting = FactoryBot.create(:planting, crop: @crop) @crop.reload - @crop.plantings.size.should eq 1 + expect(@crop.plantings.size).to eq 1 end context "wikipedia url" do @@ -127,14 +127,14 @@ describe Crop do it 'has a crop hierarchy' do @tomato = FactoryBot.create(:tomato) @roma = FactoryBot.create(:roma, parent_id: @tomato.id) - @roma.parent.should eq @tomato - @tomato.varieties.should eq [@roma] + expect(@roma.parent).to eq @tomato + expect(@tomato.varieties).to eq [@roma] end it 'toplevel scope works' do @tomato = FactoryBot.create(:tomato) @roma = FactoryBot.create(:roma, parent_id: @tomato.id) - Crop.toplevel.should eq [@tomato] + expect(Crop.toplevel).to eq [@tomato] end end @@ -145,13 +145,13 @@ describe Crop do context 'with a planting photo' do before :each do @planting = FactoryBot.create(:planting, crop: @crop) - @photo = FactoryBot.create(:photo) + @photo = FactoryBot.create(:photo, owner: @planting.owner) @planting.photos << @photo end it 'has a default photo' do @crop.default_photo.should be_an_instance_of Photo - @crop.default_photo.id.should eq @photo.id + expect(@crop.default_photo.id).to eq @photo.id end it 'is found in has_photos scope' do @@ -162,31 +162,31 @@ describe Crop do context 'with a harvest photo' do before :each do @harvest = FactoryBot.create(:harvest, crop: @crop) - @photo = FactoryBot.create(:photo) + @photo = FactoryBot.create(:photo, owner: @harvest.owner) @harvest.photos << @photo end it 'has a default photo' do @crop.default_photo.should be_an_instance_of Photo - @crop.default_photo.id.should eq @photo.id + expect(@crop.default_photo.id).to eq @photo.id end context 'and planting photo' do before :each do @planting = FactoryBot.create(:planting, crop: @crop) - @planting_photo = FactoryBot.create(:photo) + @planting_photo = FactoryBot.create(:photo, owner: @planting.owner) @planting.photos << @planting_photo end it 'should prefer the planting photo' do - @crop.default_photo.id.should eq @planting_photo.id + expect(@crop.default_photo.id).to eq @planting_photo.id end end end context 'with no plantings or harvests' do it 'has no default photo' do - @crop.default_photo.should eq nil + expect(@crop.default_photo).to eq nil end end end @@ -285,14 +285,15 @@ describe Crop do let(:crop1_planting) { crop1.plantings.first } let(:crop2_planting) { crop2.plantings.first } + let(:member) { FactoryBot.create :member, login_name: 'pikachu' } describe 'lists interesting crops' do before do # they need 3+ plantings each to be interesting - FactoryBot.create_list(:planting, 3, crop: crop1) - FactoryBot.create_list(:planting, 3, crop: crop2) + FactoryBot.create_list(:planting, 3, crop: crop1, owner: member) + FactoryBot.create_list(:planting, 3, crop: crop2, owner: member) # crops need 3+ photos to be interesting - crop1_planting.photos = FactoryBot.create_list :photo, 3 - crop2_planting.photos = FactoryBot.create_list :photo, 3 + crop1_planting.photos = FactoryBot.create_list :photo, 3, owner: member + crop2_planting.photos = FactoryBot.create_list :photo, 3, owner: member end it { is_expected.to include crop1 } @@ -303,9 +304,9 @@ describe Crop do describe 'crops without plantings are not interesting' do before do # only crop1 has plantings - FactoryBot.create_list(:planting, 3, crop: crop1) + FactoryBot.create_list(:planting, 3, crop: crop1, owner: member) # ... and photos - crop1_planting.photos = FactoryBot.create_list(:photo, 3) + crop1_planting.photos = FactoryBot.create_list(:photo, 3, owner: member) end it { is_expected.to include crop1 } it { is_expected.not_to include crop2 } @@ -315,11 +316,11 @@ describe Crop do describe 'crops without photos are not interesting' do before do # both crops have plantings - FactoryBot.create_list(:planting, 3, crop: crop1) - FactoryBot.create_list(:planting, 3, crop: crop2) + FactoryBot.create_list(:planting, 3, crop: crop1, owner: member) + FactoryBot.create_list(:planting, 3, crop: crop2, owner: member) # but only crop1 has photos - crop1_planting.photos = FactoryBot.create_list(:photo, 3) + crop1_planting.photos = FactoryBot.create_list(:photo, 3, owner: member) end it { is_expected.to include crop1 } it { is_expected.not_to include crop2 } @@ -347,7 +348,7 @@ describe Crop do @pp1 = FactoryBot.create(:plant_part) @h1 = FactoryBot.create(:harvest, crop: @maize, plant_part: @pp1) @h2 = FactoryBot.create(:harvest, crop: @maize, plant_part: @pp1) - @maize.plant_parts.should eq [@pp1] + expect(@maize.plant_parts).to eq [@pp1] end context "search", :elasticsearch do @@ -356,10 +357,10 @@ describe Crop do before { sync_elasticsearch([mushroom]) } it "finds exact matches" do - Crop.search('mushroom').should eq [mushroom] + expect(Crop.search('mushroom')).to eq [mushroom] end it "finds approximate matches" do - Crop.search('mush').should eq [mushroom] + expect(Crop.search('mush')).to eq [mushroom] end it "doesn't find non-matches" do Crop.search('mush').should_not include @crop diff --git a/spec/models/garden_spec.rb b/spec/models/garden_spec.rb index baa8c001a..346e719b5 100644 --- a/spec/models/garden_spec.rb +++ b/spec/models/garden_spec.rb @@ -67,7 +67,7 @@ describe Garden do @p1 = FactoryBot.create(:planting, crop: tomato, garden: garden, owner: garden.owner) @p2 = FactoryBot.create(:planting, crop: maize, garden: garden, owner: garden.owner) - garden.featured_plantings.should eq [@p2, @p1] + expect(garden.featured_plantings).to eq [@p2, @p1] end it "should fetch most recent 4 featured plantings" do @@ -77,7 +77,7 @@ describe Garden do @p4 = FactoryBot.create(:planting, crop: apple, garden: garden, owner: garden.owner) @p5 = FactoryBot.create(:planting, crop: walnut, garden: garden, owner: garden.owner) - garden.featured_plantings.should eq [@p5, @p4, @p3, @p2] + expect(garden.featured_plantings).to eq [@p5, @p4, @p3, @p2] end it "should skip repeated plantings" do @@ -89,7 +89,7 @@ describe Garden do @p6 = FactoryBot.create(:planting, crop: apple, garden: garden, owner: garden.owner) @p7 = FactoryBot.create(:planting, crop: pear, garden: garden, owner: garden.owner) - garden.featured_plantings.should eq [@p7, @p6, @p5, @p3] + expect(garden.featured_plantings).to eq [@p7, @p6, @p5, @p3] end end @@ -97,10 +97,10 @@ describe Garden do garden = FactoryBot.create(:garden, owner: owner) @planting1 = FactoryBot.create(:planting, garden: garden, owner: garden.owner) @planting2 = FactoryBot.create(:planting, garden: garden, owner: garden.owner) - garden.plantings.size.should eq(2) + expect(garden.plantings.size).to eq(2) all = Planting.count garden.destroy - Planting.count.should eq(all - 2) + expect(Planting.count).to eq(all - 2) end context 'area' do @@ -157,7 +157,7 @@ describe Garden do it 'sets area unit to blank if area is blank' do garden = FactoryBot.build(:garden, area: '', area_unit: 'acre') garden.should be_valid - garden.area_unit.should eq nil + expect(garden.area_unit).to eq nil end end @@ -180,16 +180,16 @@ describe Garden do p1 = FactoryBot.create(:planting, garden: garden, owner: garden.owner) p2 = FactoryBot.create(:planting, garden: garden, owner: garden.owner) - p1.finished.should eq false - p2.finished.should eq false + expect(p1.finished).to eq false + expect(p2.finished).to eq false garden.active = false garden.save p1.reload - p1.finished.should eq true + expect(p1.finished).to eq true p2.reload - p2.finished.should eq true + expect(p2.finished).to eq true end it "doesn't mark the wrong plantings as finished" do @@ -204,23 +204,23 @@ describe Garden do # plantings in that garden should be "finished" p1.reload - p1.finished.should eq true + expect(p1.finished).to eq true # plantings in other gardens should not be. p2.reload - p2.finished.should eq false + expect(p2.finished).to eq false end context 'photos' do let(:garden) { FactoryBot.create(:garden) } - let(:photo) { FactoryBot.create(:photo) } + let(:photo) { FactoryBot.create(:photo, owner: garden.owner) } before do garden.photos << photo end it 'has a photo' do - garden.photos.first.should eq photo + expect(garden.photos.first).to eq photo end it 'deletes association with photos when photo is deleted' do @@ -230,13 +230,13 @@ describe Garden do end it 'has a default photo' do - garden.default_photo.should eq photo + expect(garden.default_photo).to eq photo end it 'chooses the most recent photo' do - @photo2 = FactoryBot.create(:photo) + @photo2 = FactoryBot.create(:photo, owner: garden.owner) garden.photos << @photo2 - garden.default_photo.should eq @photo2 + expect(garden.default_photo).to eq @photo2 end end diff --git a/spec/models/harvest_spec.rb b/spec/models/harvest_spec.rb index 1944c6365..455afeb9b 100644 --- a/spec/models/harvest_spec.rb +++ b/spec/models/harvest_spec.rb @@ -61,7 +61,7 @@ describe Harvest do it 'sets unit to blank if quantity is blank' do @harvest = FactoryBot.build(:harvest, quantity: '', unit: 'individual') @harvest.should be_valid - @harvest.unit.should eq nil + expect(@harvest.unit).to eq nil end end @@ -114,7 +114,7 @@ describe Harvest do it 'sets weight_unit to blank if quantity is blank' do @harvest = FactoryBot.build(:harvest, weight_quantity: '', weight_unit: 'kg') @harvest.should be_valid - @harvest.weight_unit.should eq nil + expect(@harvest.weight_unit).to eq nil end end @@ -122,19 +122,19 @@ describe Harvest do it 'converts from pounds' do @harvest = FactoryBot.create(:harvest, weight_quantity: 2, weight_unit: "lb") @harvest.should be_valid - @harvest.reload.si_weight.should eq 0.907 + expect(@harvest.reload.si_weight).to eq 0.907 end it 'converts from ounces' do @harvest = FactoryBot.create(:harvest, weight_quantity: 16, weight_unit: "oz") @harvest.should be_valid - @harvest.reload.si_weight.should eq 0.454 + expect(@harvest.reload.si_weight).to eq 0.454 end it 'leaves kg alone' do @harvest = FactoryBot.create(:harvest, weight_quantity: 2, weight_unit: "kg") @harvest.should be_valid - @harvest.reload.si_weight.should eq 2.0 + expect(@harvest.reload.si_weight).to eq 2.0 end end @@ -142,7 +142,7 @@ describe Harvest do it 'lists most recent harvests first' do @h1 = FactoryBot.create(:harvest, created_at: 1.day.ago) @h2 = FactoryBot.create(:harvest, created_at: 1.hour.ago) - Harvest.all.order(created_at: :desc).should eq [@h2, @h1] + expect(Harvest.all.order(created_at: :desc)).to eq [@h2, @h1] end end @@ -155,7 +155,7 @@ describe Harvest do unit: nil, weight_quantity: nil, weight_unit: nil) - @h.to_s.should eq "apricots" + expect(@h.to_s).to eq "apricots" end it "1 individual apricot" do @@ -164,7 +164,7 @@ describe Harvest do unit: 'individual', weight_quantity: nil, weight_unit: nil) - @h.to_s.should eq "1 individual apricot" + expect(@h.to_s).to eq "1 individual apricot" end it "10 individual apricots" do @@ -173,7 +173,7 @@ describe Harvest do unit: 'individual', weight_quantity: nil, weight_unit: nil) - @h.to_s.should eq "10 individual apricots" + expect(@h.to_s).to eq "10 individual apricots" end it "1 bushel of apricots" do @@ -182,7 +182,7 @@ describe Harvest do unit: 'bushel', weight_quantity: nil, weight_unit: nil) - @h.to_s.should eq "1 bushel of apricots" + expect(@h.to_s).to eq "1 bushel of apricots" end it "1.5 bushels of apricots" do @@ -191,7 +191,7 @@ describe Harvest do unit: 'bushel', weight_quantity: nil, weight_unit: nil) - @h.to_s.should eq "1.5 bushels of apricots" + expect(@h.to_s).to eq "1.5 bushels of apricots" end it "10 bushels of apricots" do @@ -200,7 +200,7 @@ describe Harvest do unit: 'bushel', weight_quantity: nil, weight_unit: nil) - @h.to_s.should eq "10 bushels of apricots" + expect(@h.to_s).to eq "10 bushels of apricots" end it "apricots weighing 1.2 kg" do @@ -209,7 +209,7 @@ describe Harvest do unit: nil, weight_quantity: 1.2, weight_unit: 'kg') - @h.to_s.should eq "apricots weighing 1.2 kg" + expect(@h.to_s).to eq "apricots weighing 1.2 kg" end it "10 bushels of apricots weighing 100 kg" do @@ -218,7 +218,7 @@ describe Harvest do unit: 'bushel', weight_quantity: 100, weight_unit: 'kg') - @h.to_s.should eq "10 bushels of apricots weighing 100 kg" + expect(@h.to_s).to eq "10 bushels of apricots weighing 100 kg" end end @@ -229,26 +229,25 @@ describe Harvest do context 'without a photo' do it 'should have no default photo' do - @harvest.default_photo.should eq nil + expect(@harvest.default_photo).to eq nil end context 'and with a crop(planting) photo' do before :each do - @photo = FactoryBot.create(:photo) @planting = FactoryBot.create(:planting, crop: @harvest.crop) + @photo = FactoryBot.create(:photo, owner: @planting.owner) @planting.photos << @photo end it 'should have a default photo' do - @harvest.default_photo.should eq @photo + expect(@harvest.default_photo).to eq @photo end end end context 'with a photo' do before do - @photo = FactoryBot.create(:photo) - + @photo = FactoryBot.create(:photo, owner: @harvest.owner) @harvest.photos << @photo end @@ -257,7 +256,7 @@ describe Harvest do end it 'has a photo' do - @harvest.photos.first.should eq @photo + expect(@harvest.photos.first).to eq @photo end it 'deletes association with photos when photo is deleted' do @@ -267,29 +266,29 @@ describe Harvest do end it 'has a default photo' do - @harvest.default_photo.should eq @photo + expect(@harvest.default_photo).to eq @photo end context 'and with a crop(planting) photo' do before :each do - @crop_photo = FactoryBot.create(:photo) @planting = FactoryBot.create(:planting, crop: @harvest.crop) + @crop_photo = FactoryBot.create(:photo, owner: @planting.owner) @planting.photos << @crop_photo end it 'should prefer the harvest photo' do - @harvest.default_photo.should eq @photo + expect(@harvest.default_photo).to eq @photo end end context 'and a second photo' do before :each do - @photo2 = FactoryBot.create(:photo) + @photo2 = FactoryBot.create(:photo, owner: @harvest.owner) @harvest.photos << @photo2 end it 'chooses the most recent photo' do - @harvest.default_photo.should eq @photo2 + expect(@harvest.default_photo).to eq @photo2 end end end diff --git a/spec/models/photo_spec.rb b/spec/models/photo_spec.rb index 5f07a3e29..0e2c45a11 100644 --- a/spec/models/photo_spec.rb +++ b/spec/models/photo_spec.rb @@ -5,9 +5,9 @@ describe Photo do let(:member) { FactoryBot.create(:member) } describe 'add/delete functionality' do - let(:planting) { FactoryBot.create(:planting) } - let(:harvest) { FactoryBot.create(:harvest) } - let(:garden) { FactoryBot.create(:garden) } + let(:planting) { FactoryBot.create(:planting, owner: member) } + let(:harvest) { FactoryBot.create(:harvest, owner: member) } + let(:garden) { FactoryBot.create(:garden, owner: member) } context "adds photos" do it 'to a planting' do diff --git a/spec/models/planting_spec.rb b/spec/models/planting_spec.rb index 3c76d6ed6..194967bbd 100644 --- a/spec/models/planting_spec.rb +++ b/spec/models/planting_spec.rb @@ -313,81 +313,72 @@ describe Planting do # be done on this side, not on the photos side context 'photos' do let(:planting) { FactoryBot.create(:planting) } - let(:photo) { FactoryBot.create(:photo) } - - before do - planting.photos << photo - end + let(:photo) { FactoryBot.create(:photo, owner_id: planting.owner_id) } + before { planting.photos << photo } it 'has a photo' do - planting.photos.first.should eq photo + expect(planting.photos.first).to eq photo end it 'is found in has_photos scope' do - Planting.has_photos.should include(planting) + expect(Planting.has_photos).to include(planting) end it 'deletes association with photos when photo is deleted' do photo.destroy planting.reload - planting.photos.should be_empty + expect(planting.photos).to be_empty end it 'has a default photo' do - planting.default_photo.should eq photo + expect(planting.default_photo).to eq photo end it 'chooses the most recent photo' do - @photo2 = FactoryBot.create(:photo) + @photo2 = FactoryBot.create(:photo, owner: planting.owner) planting.photos << @photo2 - planting.default_photo.should eq @photo2 + expect(planting.default_photo).to eq @photo2 end end context 'interesting plantings' do - it 'picks up interesting plantings' do - # plantings have members created implicitly for them - # each member is different, hence these are all interesting - @planting1 = FactoryBot.create(:planting, created_at: 5.days.ago) - @planting2 = FactoryBot.create(:planting, created_at: 4.days.ago) - @planting3 = FactoryBot.create(:planting, created_at: 3.days.ago) - @planting4 = FactoryBot.create(:planting, created_at: 2.days.ago) + describe 'picks up interesting plantings' do + before do + # plantings have members created implicitly for them + # each member is different, hence these are all interesting + @planting1 = FactoryBot.create(:planting, planted_at: 5.days.ago) + @planting2 = FactoryBot.create(:planting, planted_at: 4.days.ago) + @planting3 = FactoryBot.create(:planting, planted_at: 3.days.ago) + @planting4 = FactoryBot.create(:planting, planted_at: 2.days.ago) - # plantings need photos to be interesting - @photo = FactoryBot.create(:photo) - [@planting1, @planting2, @planting3, @planting4].each do |p| - p.photos << @photo - p.save + # plantings need photos to be interesting + [@planting1, @planting2, @planting3, @planting4].each do |p| + p.photos << FactoryBot.create(:photo, owner_id: p.owner_id) + p.save + end end - [ - @planting4, - @planting3, - @planting2, - @planting1 - ].each do |p| - Planting.interesting.should include p - end + it { expect(Planting.interesting).to eq([@planting4, @planting3, @planting2, @planting1]) } end context "default arguments" do it 'ignores plantings without photos' do # first, an interesting planting @planting = FactoryBot.create(:planting) - @planting.photos << FactoryBot.create(:photo) + @planting.photos << FactoryBot.create(:photo, owner: @planting.owner) @planting.save # this one doesn't have a photo @no_photo_planting = FactoryBot.create(:planting) - Planting.interesting.should include @planting - Planting.interesting.should_not include @no_photo_planting + expect(Planting.interesting).to include @planting + expect(Planting.interesting).not_to include @no_photo_planting end it 'ignores plantings with the same owner' do # this planting is older @planting1 = FactoryBot.create(:planting, created_at: 1.day.ago) - @planting1.photos << FactoryBot.create(:photo) + @planting1.photos << FactoryBot.create(:photo, owner_id: @planting1.owner_id) @planting1.save # this one is newer, and has the same owner, through the garden @@ -395,12 +386,12 @@ describe Planting do created_at: 1.minute.ago, garden: @planting1.garden, owner: @planting1.owner) - @planting2.photos << FactoryBot.create(:photo) + @planting2.photos << FactoryBot.create(:photo, owner: @planting2.owner) @planting2.save # result: the newer one is interesting, the older one isn't - Planting.interesting.should include @planting2 - Planting.interesting.should_not include @planting1 + expect(Planting.interesting).to include @planting2 + expect(Planting.interesting).not_to include @planting1 end end @@ -408,9 +399,9 @@ describe Planting do it "only returns the number asked for" do @plantings = FactoryBot.create_list(:planting, 10) @plantings.each do |p| - p.photos << FactoryBot.create(:photo, owner: planting.owner) + p.photos << FactoryBot.create(:photo, owner: p.owner) end - Planting.interesting.limit(3).count.should eq 3 + expect(Planting.interesting.limit(3).count).to eq 3 end end end # interesting plantings diff --git a/spec/models/seed_spec.rb b/spec/models/seed_spec.rb index 690e8c75f..03634ced4 100644 --- a/spec/models/seed_spec.rb +++ b/spec/models/seed_spec.rb @@ -153,7 +153,7 @@ describe Seed do context 'photos' do let(:seed) { FactoryBot.create :seed } - before { seed.photos << FactoryBot.create(:photo) } + before { seed.photos << FactoryBot.create(:photo, owner: seed.owner) } it 'is found in has_photos scope' do Seed.has_photos.should include(seed) end @@ -161,7 +161,7 @@ describe Seed do context 'ancestry' do let(:parent_planting) { FactoryBot.create :planting } - let(:seed) { FactoryBot.create :seed, parent_planting: parent_planting } + let(:seed) { FactoryBot.create :seed, parent_planting: parent_planting, owner: parent_planting.owner } it "seed has a parent planting" do expect(seed.parent_planting).to eq(parent_planting) end diff --git a/spec/views/crops/index.html.haml_spec.rb b/spec/views/crops/index.html.haml_spec.rb index 9f2985035..7d05d18db 100644 --- a/spec/views/crops/index.html.haml_spec.rb +++ b/spec/views/crops/index.html.haml_spec.rb @@ -17,7 +17,7 @@ describe "crops/index" do it "shows photos where available" do @planting = FactoryBot.create(:planting, crop: @tomato) - @photo = FactoryBot.create(:photo) + @photo = FactoryBot.create(:photo, owner: @planting.owner) @planting.photos << @photo render assert_select "img", src: @photo.thumbnail_url