diff --git a/spec/models/ability_spec.rb b/spec/models/ability_spec.rb index 87d8a3b1d..6a054083b 100644 --- a/spec/models/ability_spec.rb +++ b/spec/models/ability_spec.rb @@ -2,36 +2,36 @@ require 'rails_helper' require 'cancan/matchers' describe Ability do - before(:each) do - @member = FactoryGirl.create(:member) - @ability = Ability.new(@member) - end + + let(:member) { FactoryGirl.create(:member) } + let(:ability) { Ability.new(member) } context "notifications" do + it 'member can view their own notifications' do - @notification = FactoryGirl.create(:notification, :recipient => @member) - @ability.should be_able_to(:read, @notification) + notification = FactoryGirl.create(:notification, :recipient => member) + ability.should be_able_to(:read, notification) end it "member can't view someone else's notifications" do - @notification = FactoryGirl.create(:notification, + notification = FactoryGirl.create(:notification, :recipient => FactoryGirl.create(:member) ) - @ability.should_not be_able_to(:read, @notification) + ability.should_not be_able_to(:read, notification) end it "member can't send messages to themself" do - @ability.should_not be_able_to(:create, + ability.should_not be_able_to(:create, FactoryGirl.create(:notification, - :recipient => @member, - :sender => @member + :recipient => member, + :sender => member ) ) end it "member can send messages to someone else" do - @ability.should be_able_to(:create, + ability.should be_able_to(:create, FactoryGirl.create(:notification, :recipient => FactoryGirl.create(:member), - :sender => @member + :sender => member ) ) end @@ -39,245 +39,240 @@ describe Ability do context "crop wrangling" do - before(:each) do - @crop = FactoryGirl.create(:crop) - end + let(:crop) { FactoryGirl.create(:crop) } context "standard member" do it "can't manage crops" do - @ability.should_not be_able_to(:update, @crop) - @ability.should_not be_able_to(:destroy, @crop) + ability.should_not be_able_to(:update, crop) + ability.should_not be_able_to(:destroy, crop) end it "can request crops" do - @ability.should be_able_to(:create, Crop) + ability.should be_able_to(:create, Crop) end it "can read crops" do - @ability.should be_able_to(:read, @crop) + ability.should be_able_to(:read, crop) end end context "crop wrangler" do + + let(:role) { FactoryGirl.create(:crop_wrangler) } + before(:each) do - @role = FactoryGirl.create(:crop_wrangler) - @member.roles << @role - @cw_ability = Ability.new(@member) + member.roles << role end it "has crop_wrangler role" do - @member.has_role?(:crop_wrangler).should be true + member.has_role?(:crop_wrangler).should be true end it "can create crops" do - @cw_ability.should be_able_to(:create, Crop) + ability.should be_able_to(:create, Crop) end it "can update crops" do - @cw_ability.should be_able_to(:update, @crop) + ability.should be_able_to(:update, crop) end it "can destroy crops" do - @cw_ability.should be_able_to(:destroy, @crop) + ability.should be_able_to(:destroy, crop) end end end context "products" do - before(:each) do - @product = FactoryGirl.create(:product) - end + let(:product) { FactoryGirl.create(:product) } context "standard member" do it "can't read or manage products" do - @ability.should_not be_able_to(:read, @product) - @ability.should_not be_able_to(:create, Product) - @ability.should_not be_able_to(:update, @product) - @ability.should_not be_able_to(:destroy, @product) + ability.should_not be_able_to(:read, product) + ability.should_not be_able_to(:create, Product) + ability.should_not be_able_to(:update, product) + ability.should_not be_able_to(:destroy, product) end end context "admin" do - before(:each) do - @role = FactoryGirl.create(:admin) - @member.roles << @role - @admin_ability = Ability.new(@member) + + let(:role) { FactoryGirl.create(:admin) } + + before do + member.roles << role end it "has admin role" do - @member.has_role?(:admin).should be true + member.has_role?(:admin).should be true end it "can read products" do - @admin_ability.should be_able_to(:read, @product) + ability.should be_able_to(:read, product) end it "can create products" do - @admin_ability.should be_able_to(:create, Product) + ability.should be_able_to(:create, Product) end it "can update products" do - @admin_ability.should be_able_to(:update, @product) + ability.should be_able_to(:update, product) end it "can destroy products" do - @admin_ability.should be_able_to(:destroy, @product) + ability.should be_able_to(:destroy, product) end end end context "orders" do - before(:each) do - @order = FactoryGirl.create(:order, :member => @member) - @strangers_order = FactoryGirl.create(:order, - :member => FactoryGirl.create(:member)) - @completed_order = FactoryGirl.create(:completed_order, - :member => @member) - - @order_item = FactoryGirl.create(:order_item, :order => @order) - @strangers_order_item = FactoryGirl.create(:order_item, - :order => @strangers_order) - @completed_order_item = FactoryGirl.create(:order_item, - :order => @completed_order) - end + let(:order) { FactoryGirl.create(:order, :member => member) } + let(:strangers_order) { FactoryGirl.create(:order, + :member => FactoryGirl.create(:member)) } + let(:completed_order) { FactoryGirl.create(:completed_order, + :member => member) } + let(:order_item) { FactoryGirl.create(:order_item, :order => order) } + let(:strangers_order_item) { FactoryGirl.create(:order_item, + :order => strangers_order) } + let(:completed_order_item) { FactoryGirl.create(:order_item, + :order => completed_order) } context "standard member" do it "can read their own orders" do - @ability.should be_able_to(:read, @order) - @ability.should be_able_to(:read, @completed_order) + ability.should be_able_to(:read, order) + ability.should be_able_to(:read, completed_order) end it "can't read other people's orders" do - @ability.should_not be_able_to(:read, @strangers_order) + ability.should_not be_able_to(:read, strangers_order) end it "can create a new order" do - @ability.should be_able_to(:create, Order) + ability.should be_able_to(:create, Order) end it "can complete their own current order" do - @ability.should be_able_to(:complete, @order) + ability.should be_able_to(:complete, order) end it "can't complete someone else's order" do - @ability.should_not be_able_to(:complete, @strangers_order) + ability.should_not be_able_to(:complete, strangers_order) end it "can't complete a completed order" do - @ability.should_not be_able_to(:complete, @completed_order) + ability.should_not be_able_to(:complete, completed_order) end it "can delete a current order" do - @ability.should be_able_to(:destroy, @order) + ability.should be_able_to(:destroy, order) end it "can't delete someone else's order" do - @ability.should_not be_able_to(:destroy, @strangers_order) + ability.should_not be_able_to(:destroy, strangers_order) end it "can't delete a completed order" do - @ability.should_not be_able_to(:destroy, @completed_order) + ability.should_not be_able_to(:destroy, completed_order) end it "can't read their own order items" do - @ability.should_not be_able_to(:read, @order_item) - @ability.should_not be_able_to(:read, @completed_order_item) + ability.should_not be_able_to(:read, order_item) + ability.should_not be_able_to(:read, completed_order_item) end it "can't read other people's order items" do - @ability.should_not be_able_to(:read, @strangers_order_item) + ability.should_not be_able_to(:read, strangers_order_item) end it "can create a new order item" do - @ability.should be_able_to(:create, OrderItem) + ability.should be_able_to(:create, OrderItem) end it "can't update their own order items" do - @ability.should_not be_able_to(:update, @order_item) + ability.should_not be_able_to(:update, order_item) end it "can't update other people's order items" do - @ability.should_not be_able_to(:update, @strangers_order_item) + ability.should_not be_able_to(:update, strangers_order_item) end it "can't updated items in completed orders" do - @ability.should_not be_able_to(:update, @completed_order_item) + ability.should_not be_able_to(:update, completed_order_item) end it "can't delete their own order item" do - @ability.should_not be_able_to(:destroy, @order_item) + ability.should_not be_able_to(:destroy, order_item) end it "can't delete someone else's order item" do - @ability.should_not be_able_to(:destroy, @strangers_order_item) + ability.should_not be_able_to(:destroy, strangers_order_item) end it "can't delete items from completed orders" do - @ability.should_not be_able_to(:destroy, @completed_order_item) + ability.should_not be_able_to(:destroy, completed_order_item) end end context "admin" do - before(:each) do - @role = FactoryGirl.create(:admin) - @member.roles << @role - @admin_ability = Ability.new(@member) + + let(:role) { FactoryGirl.create(:admin) } + + before do + member.roles << role end it "has admin role" do - @member.has_role?(:admin).should be true + member.has_role?(:admin).should be true end it "can read orders" do - @admin_ability.should be_able_to(:read, @order) + ability.should be_able_to(:read, order) end it "cannot create orders" do - @admin_ability.should_not be_able_to(:create, @order) + ability.should_not be_able_to(:create, order) end it "cannot complete orders" do - @admin_ability.should_not be_able_to(:complete, @order) + ability.should_not be_able_to(:complete, order) end it "cannot delete orders" do - @admin_ability.should_not be_able_to(:destroy, @order) + ability.should_not be_able_to(:destroy, order) end end end context 'account details' do - before(:each) do - @account = @member.account - end + + let(:account) { member.account } context 'ordinary member' do it "can't read account details" do - @ability.should_not be_able_to(:read, @account) + ability.should_not be_able_to(:read, account) end it "can't manage account details" do - @ability.should_not be_able_to(:create, Account) - @ability.should_not be_able_to(:update, @account) - @ability.should_not be_able_to(:destroy, @account) + ability.should_not be_able_to(:create, Account) + ability.should_not be_able_to(:update, account) + ability.should_not be_able_to(:destroy, account) end end context 'admin' do - before(:each) do - @role = FactoryGirl.create(:admin) - @member.roles << @role - @admin_ability = Ability.new(@member) + let(:role) { FactoryGirl.create(:admin) } + + before do + member.roles << role end it "can read account details" do - @admin_ability.should be_able_to(:read, @account) + ability.should be_able_to(:read, account) end it "can manage account details" do - @admin_ability.should be_able_to(:create, Account) - @admin_ability.should be_able_to(:update, @account) - @admin_ability.should be_able_to(:destroy, @account) + ability.should be_able_to(:create, Account) + ability.should be_able_to(:update, account) + ability.should be_able_to(:destroy, account) end end @@ -285,44 +280,43 @@ describe Ability do end context 'plant parts' do - before(:each) do - @plant_part = FactoryGirl.create(:plant_part) - end + + let(:plant_part) { FactoryGirl.create(:plant_part) } context 'ordinary member' do it "can read plant parts" do - @ability.should be_able_to(:read, @plant_part) + ability.should be_able_to(:read, plant_part) end it "can't manage plant parts" do - @ability.should_not be_able_to(:create, PlantPart) - @ability.should_not be_able_to(:update, @plant_part) - @ability.should_not be_able_to(:destroy, @plant_part) + ability.should_not be_able_to(:create, PlantPart) + ability.should_not be_able_to(:update, plant_part) + ability.should_not be_able_to(:destroy, plant_part) end end context 'admin' do - before(:each) do - @role = FactoryGirl.create(:admin) - @member.roles << @role - @admin_ability = Ability.new(@member) + let(:role) { FactoryGirl.create(:admin) } + + before do + member.roles << role end it "can read plant_part details" do - @admin_ability.should be_able_to(:read, @plant_part) + ability.should be_able_to(:read, plant_part) end it "can manage plant_part details" do - @admin_ability.should be_able_to(:create, PlantPart) - @admin_ability.should be_able_to(:update, @plant_part) + ability.should be_able_to(:create, PlantPart) + ability.should be_able_to(:update, plant_part) end it "can delete an unused plant part" do - @admin_ability.should be_able_to(:destroy, @plant_part) + ability.should be_able_to(:destroy, plant_part) end it "can't delete a plant part that has harvests" do - @harvest = FactoryGirl.create(:harvest, :plant_part => @plant_part) - @admin_ability.should_not be_able_to(:destroy, @plant_part) + @harvest = FactoryGirl.create(:harvest, :plant_part => plant_part) + ability.should_not be_able_to(:destroy, plant_part) end end diff --git a/spec/models/account_spec.rb b/spec/models/account_spec.rb index b1765ccc0..2a41ed983 100644 --- a/spec/models/account_spec.rb +++ b/spec/models/account_spec.rb @@ -1,30 +1,29 @@ require 'rails_helper' describe Account do - before(:each) do - @member = FactoryGirl.create(:member) - end + + let(:member) { FactoryGirl.create(:member) } it "auto-creates an account detail record when a member is created" do - @member.account.should be_an_instance_of Account + member.account.should be_an_instance_of Account end it "won't let you create two account details for the same member" do - @details = Account.new(:member_id => @member.id) + @details = Account.new(:member_id => member.id) @details.should_not be_valid end it "formats the 'paid until' date nicely" do - @member.account.account_type = FactoryGirl.create(:account_type) - @member.account.paid_until_string.should eq nil + member.account.account_type = FactoryGirl.create(:account_type) + member.account.paid_until_string.should eq nil - @member.account.account_type = FactoryGirl.create(:permanent_paid_account_type) - @member.account.paid_until_string.should eq "forever" + member.account.account_type = FactoryGirl.create(:permanent_paid_account_type) + member.account.paid_until_string.should eq "forever" - @member.account.account_type = FactoryGirl.create(:paid_account_type) + member.account.account_type = FactoryGirl.create(:paid_account_type) @time = Time.zone.now - @member.account.paid_until = @time - @member.account.paid_until_string.should eq @time.to_s + member.account.paid_until = @time + member.account.paid_until_string.should eq @time.to_s end end diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb index d54cd5606..9fbf1ca1e 100644 --- a/spec/models/comment_spec.rb +++ b/spec/models/comment_spec.rb @@ -3,23 +3,21 @@ require 'rails_helper' describe Comment do context "basic" do - before(:each) do - @comment = FactoryGirl.create(:comment) - end + + let(:comment) { FactoryGirl.create(:comment) } it "belongs to a post" do - @comment.post.should be_an_instance_of Post + comment.post.should be_an_instance_of Post end it "belongs to an author" do - @comment.author.should be_an_instance_of Member + comment.author.should be_an_instance_of Member end end context "notifications" do - before(:each) do - @comment = FactoryGirl.create(:comment) - end + + let(:comment) { FactoryGirl.create(:comment) } it "sends a notification when a comment is posted" do expect { diff --git a/spec/models/crop_spec.rb b/spec/models/crop_spec.rb index 7423a4fd2..724567626 100644 --- a/spec/models/crop_spec.rb +++ b/spec/models/crop_spec.rb @@ -3,42 +3,40 @@ require 'rails_helper' describe Crop do context 'all fields present' do - before(:each) do - @crop = FactoryGirl.create(:tomato) - end + let(:crop) { FactoryGirl.create(:tomato) } it 'should save a basic crop' do - @crop.save.should be(true) + crop.save.should be(true) end it 'should be fetchable from the database' do - @crop.save + crop.save @crop2 = Crop.find_by_name('tomato') @crop2.en_wikipedia_url.should == "http://en.wikipedia.org/wiki/Tomato" @crop2.slug.should == "tomato" end it 'should stringify as the system name' do - @crop.save - @crop.to_s.should == 'tomato' - "#{@crop}".should == 'tomato' + crop.save + crop.to_s.should == 'tomato' + "#{crop}".should == 'tomato' end it 'has a creator' do - @crop.save - @crop.creator.should be_an_instance_of Member + crop.save + crop.creator.should be_an_instance_of Member end end context 'invalid data' do it 'should not save a crop without a system name' do - @crop = FactoryGirl.build(:crop, :name => nil) - expect { @crop.save }.to raise_error ActiveRecord::StatementInvalid + crop = FactoryGirl.build(:crop, :name => nil) + expect { crop.save }.to raise_error ActiveRecord::StatementInvalid end end context 'ordering' do - before(:each) do + before do @uppercase = FactoryGirl.create(:uppercasecrop, :created_at => 1.minute.ago) @lowercase = FactoryGirl.create(:lowercasecrop, :created_at => 2.days.ago) end @@ -53,18 +51,20 @@ describe Crop do end context 'popularity' do - before (:each) do - @tomato = FactoryGirl.create(:tomato) - @maize = FactoryGirl.create(:maize) - @cucumber = FactoryGirl.create(:crop, :name => 'cucumber') - FactoryGirl.create_list(:planting, 10, :crop => @maize) - FactoryGirl.create_list(:planting, 3, :crop => @tomato) + + let(:tomato) { FactoryGirl.create(:tomato) } + let(:maize) { FactoryGirl.create(:maize) } + let(:cucumber) { FactoryGirl.create(:crop, :name => 'cucumber') } + + before do + FactoryGirl.create_list(:planting, 10, :crop => maize) + FactoryGirl.create_list(:planting, 3, :crop => tomato) end it "sorts by most plantings" do - Crop.popular.first.should eq @maize - FactoryGirl.create_list(:planting, 10, :crop => @tomato) - Crop.popular.first.should eq @tomato + Crop.popular.first.should eq maize + FactoryGirl.create_list(:planting, 10, :crop => tomato) + Crop.popular.first.should eq tomato end end @@ -118,70 +118,67 @@ describe Crop do end context 'sunniness' do - before(:each) do - @crop = FactoryGirl.create(:tomato) - end + + let(:crop) { FactoryGirl.create(:tomato) } it 'returns a hash of sunniness values' do - planting1 = FactoryGirl.create(:sunny_planting, :crop => @crop) - planting2 = FactoryGirl.create(:sunny_planting, :crop => @crop) - planting3 = FactoryGirl.create(:semi_shady_planting, :crop => @crop) - planting4 = FactoryGirl.create(:shady_planting, :crop => @crop) - @crop.sunniness.should be_an_instance_of Hash + planting1 = FactoryGirl.create(:sunny_planting, :crop => crop) + planting2 = FactoryGirl.create(:sunny_planting, :crop => crop) + planting3 = FactoryGirl.create(:semi_shady_planting, :crop => crop) + planting4 = FactoryGirl.create(:shady_planting, :crop => crop) + crop.sunniness.should be_an_instance_of Hash end it 'counts each sunniness value' do - planting1 = FactoryGirl.create(:sunny_planting, :crop => @crop) - planting2 = FactoryGirl.create(:sunny_planting, :crop => @crop) - planting3 = FactoryGirl.create(:semi_shady_planting, :crop => @crop) - planting4 = FactoryGirl.create(:shady_planting, :crop => @crop) - @crop.sunniness.should == { 'sun' => 2, 'shade' => 1, 'semi-shade' => 1 } + planting1 = FactoryGirl.create(:sunny_planting, :crop => crop) + planting2 = FactoryGirl.create(:sunny_planting, :crop => crop) + planting3 = FactoryGirl.create(:semi_shady_planting, :crop => crop) + planting4 = FactoryGirl.create(:shady_planting, :crop => crop) + crop.sunniness.should == { 'sun' => 2, 'shade' => 1, 'semi-shade' => 1 } end it 'ignores unused sunniness values' do - planting1 = FactoryGirl.create(:sunny_planting, :crop => @crop) - planting2 = FactoryGirl.create(:sunny_planting, :crop => @crop) - planting3 = FactoryGirl.create(:semi_shady_planting, :crop => @crop) - @crop.sunniness.should == { 'sun' => 2, 'semi-shade' => 1 } + planting1 = FactoryGirl.create(:sunny_planting, :crop => crop) + planting2 = FactoryGirl.create(:sunny_planting, :crop => crop) + planting3 = FactoryGirl.create(:semi_shady_planting, :crop => crop) + crop.sunniness.should == { 'sun' => 2, 'semi-shade' => 1 } end end context 'planted_from' do - before(:each) do - @crop = FactoryGirl.create(:tomato) - end + + let(:crop) { FactoryGirl.create(:tomato) } it 'returns a hash of sunniness values' do - planting1 = FactoryGirl.create(:seed_planting, :crop => @crop) - planting2 = FactoryGirl.create(:seed_planting, :crop => @crop) - planting3 = FactoryGirl.create(:seedling_planting, :crop => @crop) - planting4 = FactoryGirl.create(:cutting_planting, :crop => @crop) - @crop.planted_from.should be_an_instance_of Hash + planting1 = FactoryGirl.create(:seed_planting, :crop => crop) + planting2 = FactoryGirl.create(:seed_planting, :crop => crop) + planting3 = FactoryGirl.create(:seedling_planting, :crop => crop) + planting4 = FactoryGirl.create(:cutting_planting, :crop => crop) + crop.planted_from.should be_an_instance_of Hash end it 'counts each planted_from value' do - planting1 = FactoryGirl.create(:seed_planting, :crop => @crop) - planting2 = FactoryGirl.create(:seed_planting, :crop => @crop) - planting3 = FactoryGirl.create(:seedling_planting, :crop => @crop) - planting4 = FactoryGirl.create(:cutting_planting, :crop => @crop) - @crop.planted_from.should == { 'seed' => 2, 'seedling' => 1, 'cutting' => 1 } + planting1 = FactoryGirl.create(:seed_planting, :crop => crop) + planting2 = FactoryGirl.create(:seed_planting, :crop => crop) + planting3 = FactoryGirl.create(:seedling_planting, :crop => crop) + planting4 = FactoryGirl.create(:cutting_planting, :crop => crop) + crop.planted_from.should == { 'seed' => 2, 'seedling' => 1, 'cutting' => 1 } end it 'ignores unused planted_from values' do - planting1 = FactoryGirl.create(:seed_planting, :crop => @crop) - planting2 = FactoryGirl.create(:seed_planting, :crop => @crop) - planting3 = FactoryGirl.create(:seedling_planting, :crop => @crop) - @crop.planted_from.should == { 'seed' => 2, 'seedling' => 1 } + planting1 = FactoryGirl.create(:seed_planting, :crop => crop) + planting2 = FactoryGirl.create(:seed_planting, :crop => crop) + planting3 = FactoryGirl.create(:seedling_planting, :crop => crop) + crop.planted_from.should == { 'seed' => 2, 'seedling' => 1 } end end context 'popular plant parts' do - before(:each) do - @crop = FactoryGirl.create(:tomato) - end + + let(:crop) { FactoryGirl.create(:tomato) } it 'returns a hash of plant_part values' do - @crop.popular_plant_parts.should be_an_instance_of Hash + crop.popular_plant_parts.should be_an_instance_of Hash end it 'counts each plant_part value' do @@ -190,22 +187,22 @@ describe Crop do @root = FactoryGirl.create(:plant_part) @bulb = FactoryGirl.create(:plant_part) @harvest1 = FactoryGirl.create(:harvest, - :crop => @crop, + :crop => crop, :plant_part => @fruit ) @harvest2 = FactoryGirl.create(:harvest, - :crop => @crop, + :crop => crop, :plant_part => @fruit ) @harvest3 = FactoryGirl.create(:harvest, - :crop => @crop, + :crop => crop, :plant_part => @seed ) @harvest4 = FactoryGirl.create(:harvest, - :crop => @crop, + :crop => crop, :plant_part => @root ) - @crop.popular_plant_parts.should == { @fruit => 2, @seed => 1, @root => 1 } + crop.popular_plant_parts.should == { @fruit => 2, @seed => 1, @root => 1 } end end @@ -327,21 +324,24 @@ describe Crop do end context "search" do - before :each do - @mushroom = FactoryGirl.create(:crop, :name => 'mushroom') - sync_elasticsearch([@mushroom]) + + let(:mushroom) { FactoryGirl.create(:crop, :name => 'mushroom') } + + before do + sync_elasticsearch([mushroom]) end + it "finds exact matches" do - Crop.search('mushroom').should eq [@mushroom] + Crop.search('mushroom').should eq [mushroom] end it "finds approximate matches" do - Crop.search('mush').should eq [@mushroom] + Crop.search('mush').should eq [mushroom] end it "doesn't find non-matches" do Crop.search('mush').should_not include @crop end it "searches case insensitively" do - Crop.search('mUsH').should include @mushroom + Crop.search('mUsH').should include mushroom end it "doesn't find 'rejected' crop" do @rejected_crop = FactoryGirl.create(:rejected_crop, :name => 'tomato') diff --git a/spec/models/forum_spec.rb b/spec/models/forum_spec.rb index 9769882c6..dfebfa356 100644 --- a/spec/models/forum_spec.rb +++ b/spec/models/forum_spec.rb @@ -1,32 +1,31 @@ require 'rails_helper' describe Forum do - before(:each) do - @forum = FactoryGirl.create(:forum) - end + + let(:forum) { FactoryGirl.create(:forum) } it "belongs to an owner" do - @forum.owner.should be_an_instance_of Member + forum.owner.should be_an_instance_of Member end it "stringifies nicely" do - "#{@forum}".should eq @forum.name + "#{forum}".should eq forum.name end it 'has a slug' do - @forum.slug.should eq 'permaculture' + forum.slug.should eq 'permaculture' end it "has many posts" do - @post1 = FactoryGirl.create(:forum_post, :forum => @forum) - @post2 = FactoryGirl.create(:forum_post, :forum => @forum) - @forum.posts.length.should == 2 + @post1 = FactoryGirl.create(:forum_post, :forum => forum) + @post2 = FactoryGirl.create(:forum_post, :forum => forum) + forum.posts.length.should == 2 end it "orders posts in reverse chron order" do - @post1 = FactoryGirl.create(:forum_post, :forum => @forum, :created_at => 2.days.ago) - @post2 = FactoryGirl.create(:forum_post, :forum => @forum, :created_at => 1.day.ago) - @forum.posts.first.should eq @post2 + @post1 = FactoryGirl.create(:forum_post, :forum => forum, :created_at => 2.days.ago) + @post2 = FactoryGirl.create(:forum_post, :forum => forum, :created_at => 1.day.ago) + forum.posts.first.should eq @post2 end end diff --git a/spec/models/garden_spec.rb b/spec/models/garden_spec.rb index 7a6590596..6eebba460 100644 --- a/spec/models/garden_spec.rb +++ b/spec/models/garden_spec.rb @@ -1,80 +1,78 @@ require 'rails_helper' describe Garden do - before :each do - @owner = FactoryGirl.create(:member) - @garden = FactoryGirl.create(:garden, :owner => @owner) - end + + let(:owner) { FactoryGirl.create(:member) } + let(:garden) { FactoryGirl.create(:garden, :owner => owner) } it "should have a slug" do - @garden.slug.should match(/member\d+-springfield-community-garden/) + garden.slug.should match(/member\d+-springfield-community-garden/) end it "should have a description" do - @garden.description.should == "This is a **totally** cool garden" + garden.description.should == "This is a **totally** cool garden" end it "doesn't allow a nil name" do - @garden = FactoryGirl.build(:garden, :name => nil) - @garden.should_not be_valid + garden = FactoryGirl.build(:garden, :name => nil) + garden.should_not be_valid end it "doesn't allow a blank name" do - @garden = FactoryGirl.build(:garden, :name => "") - @garden.should_not be_valid + garden = FactoryGirl.build(:garden, :name => "") + garden.should_not be_valid end it "doesn't allow a name with only spaces" do - @garden = FactoryGirl.build(:garden, :name => " ") - @garden.should_not be_valid + garden = FactoryGirl.build(:garden, :name => " ") + garden.should_not be_valid end it "should have an owner" do - @garden.owner.should be_an_instance_of Member + garden.owner.should be_an_instance_of Member end it "should stringify as its name" do - @garden.to_s.should == @garden.name + garden.to_s.should == garden.name end context "featured plantings" do - before :each do - @tomato = FactoryGirl.create(:tomato) - @maize = FactoryGirl.create(:maize) - @chard = FactoryGirl.create(:chard) - @apple = FactoryGirl.create(:apple) - @pear = FactoryGirl.create(:pear) - @walnut = FactoryGirl.create(:walnut) - end + + let(:tomato) { FactoryGirl.create(:tomato) } + let(:maize) { FactoryGirl.create(:maize) } + let(:chard) { FactoryGirl.create(:chard) } + let(:apple) { FactoryGirl.create(:apple) } + let(:pear) { FactoryGirl.create(:pear) } + let(:walnut) { FactoryGirl.create(:walnut) } it "should fetch < 4 featured plantings if insufficient exist" do - @p1 = FactoryGirl.create(:planting, :crop => @tomato, :garden => @garden) - @p2 = FactoryGirl.create(:planting, :crop => @maize, :garden => @garden) + @p1 = FactoryGirl.create(:planting, :crop => tomato, :garden => garden) + @p2 = FactoryGirl.create(:planting, :crop => maize, :garden => garden) - @garden.featured_plantings.should eq [@p2, @p1] + garden.featured_plantings.should eq [@p2, @p1] end it "should fetch most recent 4 featured plantings" do - @p1 = FactoryGirl.create(:planting, :crop => @tomato, :garden => @garden) - @p2 = FactoryGirl.create(:planting, :crop => @maize, :garden => @garden) - @p3 = FactoryGirl.create(:planting, :crop => @chard, :garden => @garden) - @p4 = FactoryGirl.create(:planting, :crop => @apple, :garden => @garden) - @p5 = FactoryGirl.create(:planting, :crop => @walnut, :garden => @garden) + @p1 = FactoryGirl.create(:planting, :crop => tomato, :garden => garden) + @p2 = FactoryGirl.create(:planting, :crop => maize, :garden => garden) + @p3 = FactoryGirl.create(:planting, :crop => chard, :garden => garden) + @p4 = FactoryGirl.create(:planting, :crop => apple, :garden => garden) + @p5 = FactoryGirl.create(:planting, :crop => walnut, :garden => garden) - @garden.featured_plantings.should eq [@p5, @p4, @p3, @p2] + garden.featured_plantings.should eq [@p5, @p4, @p3, @p2] end it "should skip repeated plantings" do - @p1 = FactoryGirl.create(:planting, :crop => @tomato, :garden => @garden) - @p2 = FactoryGirl.create(:planting, :crop => @maize, :garden => @garden) - @p3 = FactoryGirl.create(:planting, :crop => @chard, :garden => @garden) - @p4 = FactoryGirl.create(:planting, :crop => @apple, :garden => @garden) - @p5 = FactoryGirl.create(:planting, :crop => @walnut, :garden => @garden) - @p6 = FactoryGirl.create(:planting, :crop => @apple, :garden => @garden) - @p7 = FactoryGirl.create(:planting, :crop => @pear, :garden => @garden) + @p1 = FactoryGirl.create(:planting, :crop => tomato, :garden => garden) + @p2 = FactoryGirl.create(:planting, :crop => maize, :garden => garden) + @p3 = FactoryGirl.create(:planting, :crop => chard, :garden => garden) + @p4 = FactoryGirl.create(:planting, :crop => apple, :garden => garden) + @p5 = FactoryGirl.create(:planting, :crop => walnut, :garden => garden) + @p6 = FactoryGirl.create(:planting, :crop => apple, :garden => garden) + @p7 = FactoryGirl.create(:planting, :crop => pear, :garden => garden) - @garden.featured_plantings.should eq [@p7, @p6, @p5, @p3] + garden.featured_plantings.should eq [@p7, @p6, @p5, @p3] end end @@ -87,86 +85,85 @@ describe Garden do end it "destroys plantings when deleted" do - @garden = FactoryGirl.create(:garden, :owner => @owner) - @planting1 = FactoryGirl.create(:planting, :garden => @garden) - @planting2 = FactoryGirl.create(:planting, :garden => @garden) - @garden.plantings.length.should == 2 + garden = FactoryGirl.create(:garden, :owner => owner) + @planting1 = FactoryGirl.create(:planting, :garden => garden) + @planting2 = FactoryGirl.create(:planting, :garden => garden) + garden.plantings.length.should == 2 all = Planting.count - @garden.destroy + garden.destroy Planting.count.should == all - 2 end context 'area' do it 'allows numeric area' do - @garden = FactoryGirl.build(:garden, :area => 33) - @garden.should be_valid + garden = FactoryGirl.build(:garden, :area => 33) + garden.should be_valid end it "doesn't allow negative area" do - @garden = FactoryGirl.build(:garden, :area => -5) - @garden.should_not be_valid + garden = FactoryGirl.build(:garden, :area => -5) + garden.should_not be_valid end it 'allows decimal quantities' do - @garden = FactoryGirl.build(:garden, :area => 3.3) - @garden.should be_valid + garden = FactoryGirl.build(:garden, :area => 3.3) + garden.should be_valid end it 'allows blank quantities' do - @garden = FactoryGirl.build(:garden, :area => '') - @garden.should be_valid + garden = FactoryGirl.build(:garden, :area => '') + garden.should be_valid end it 'allows nil quantities' do - @garden = FactoryGirl.build(:garden, :area => nil) - @garden.should be_valid + garden = FactoryGirl.build(:garden, :area => nil) + garden.should be_valid end it 'cleans up zero quantities' do - @garden = FactoryGirl.build(:garden, :area => 0) - @garden.area.should == 0 + garden = FactoryGirl.build(:garden, :area => 0) + garden.area.should == 0 end it "doesn't allow non-numeric quantities" do - @garden = FactoryGirl.build(:garden, :area => "99a") - @garden.should_not be_valid + garden = FactoryGirl.build(:garden, :area => "99a") + garden.should_not be_valid end end context 'units' do Garden::AREA_UNITS_VALUES.values.push(nil, '').each do |s| it "#{s} should be a valid unit" do - @garden = FactoryGirl.build(:garden, :area_unit => s) - @garden.should be_valid + garden = FactoryGirl.build(:garden, :area_unit => s) + garden.should be_valid end end it 'should refuse invalid unit values' do - @garden = FactoryGirl.build(:garden, :area_unit => 'not valid') - @garden.should_not be_valid - @garden.errors[:area_unit].should include("not valid is not a valid area unit") + garden = FactoryGirl.build(:garden, :area_unit => 'not valid') + garden.should_not be_valid + garden.errors[:area_unit].should include("not valid is not a valid area unit") end it 'sets area unit to blank if area is blank' do - @garden = FactoryGirl.build(:garden, :area => '', :area_unit => 'acre') - @garden.should be_valid - @garden.area_unit.should eq nil + garden = FactoryGirl.build(:garden, :area => '', :area_unit => 'acre') + garden.should be_valid + garden.area_unit.should eq nil end end context 'active scopes' do - before(:each) do - @active = FactoryGirl.create(:garden) - @inactive = FactoryGirl.create(:inactive_garden) - end + + let(:active) { FactoryGirl.create(:garden) } + let(:inactive) { FactoryGirl.create(:inactive_garden) } it 'includes active garden in active scope' do - Garden.active.should include @active - Garden.active.should_not include @inactive + Garden.active.should include active + Garden.active.should_not include inactive end it 'includes inactive garden in inactive scope' do - Garden.inactive.should include @inactive - Garden.inactive.should_not include @active + Garden.inactive.should include inactive + Garden.inactive.should_not include active end end @@ -207,30 +204,32 @@ describe Garden do end context 'photos' do - before(:each) do - @garden = FactoryGirl.create(:garden) - @photo = FactoryGirl.create(:photo) - @garden.photos << @photo + + let(:garden) { FactoryGirl.create(:garden) } + let(:photo) { FactoryGirl.create(:photo) } + + before do + garden.photos << photo end it 'has a photo' do - @garden.photos.first.should eq @photo + garden.photos.first.should eq photo end it 'deletes association with photos when photo is deleted' do - @photo.destroy - @garden.reload - @garden.photos.should be_empty + photo.destroy + garden.reload + garden.photos.should be_empty end it 'has a default photo' do - @garden.default_photo.should eq @photo + garden.default_photo.should eq photo end it 'chooses the most recent photo' do @photo2 = FactoryGirl.create(:photo) - @garden.photos << @photo2 - @garden.default_photo.should eq @photo2 + garden.photos << @photo2 + garden.default_photo.should eq @photo2 end end diff --git a/spec/models/harvest_spec.rb b/spec/models/harvest_spec.rb index cd6d5f1ef..f8e2c4f5a 100644 --- a/spec/models/harvest_spec.rb +++ b/spec/models/harvest_spec.rb @@ -147,12 +147,11 @@ describe Harvest do end context "stringification" do - before :each do - @crop = FactoryGirl.create(:crop, :name => "apricot") - end + + let(:crop) { FactoryGirl.create(:crop, :name => "apricot") } it "apricots" do - @h = FactoryGirl.create(:harvest, :crop => @crop, + @h = FactoryGirl.create(:harvest, :crop => crop, :quantity => nil, :unit => nil, :weight_quantity => nil, @@ -162,7 +161,7 @@ describe Harvest do end it "1 individual apricot" do - @h = FactoryGirl.create(:harvest, :crop => @crop, + @h = FactoryGirl.create(:harvest, :crop => crop, :quantity => 1, :unit => 'individual', :weight_quantity => nil, @@ -172,7 +171,7 @@ describe Harvest do end it "10 individual apricots" do - @h = FactoryGirl.create(:harvest, :crop => @crop, + @h = FactoryGirl.create(:harvest, :crop => crop, :quantity => 10, :unit => 'individual', :weight_quantity => nil, @@ -182,7 +181,7 @@ describe Harvest do end it "1 bushel of apricots" do - @h = FactoryGirl.create(:harvest, :crop => @crop, + @h = FactoryGirl.create(:harvest, :crop => crop, :quantity => 1, :unit => 'bushel', :weight_quantity => nil, @@ -192,7 +191,7 @@ describe Harvest do end it "1.5 bushels of apricots" do - @h = FactoryGirl.create(:harvest, :crop => @crop, + @h = FactoryGirl.create(:harvest, :crop => crop, :quantity => 1.5, :unit => 'bushel', :weight_quantity => nil, @@ -202,7 +201,7 @@ describe Harvest do end it "10 bushels of apricots" do - @h = FactoryGirl.create(:harvest, :crop => @crop, + @h = FactoryGirl.create(:harvest, :crop => crop, :quantity => 10, :unit => 'bushel', :weight_quantity => nil, @@ -212,7 +211,7 @@ describe Harvest do end it "apricots weighing 1.2 kg" do - @h = FactoryGirl.create(:harvest, :crop => @crop, + @h = FactoryGirl.create(:harvest, :crop => crop, :quantity => nil, :unit => nil, :weight_quantity => 1.2, @@ -222,7 +221,7 @@ describe Harvest do end it "10 bushels of apricots weighing 100 kg" do - @h = FactoryGirl.create(:harvest, :crop => @crop, + @h = FactoryGirl.create(:harvest, :crop => crop, :quantity => 10, :unit => 'bushel', :weight_quantity => 100, @@ -233,30 +232,32 @@ describe Harvest do end context 'photos' do - before(:each) do - @harvest = FactoryGirl.create(:harvest) - @photo = FactoryGirl.create(:photo) - @harvest.photos << @photo + + let(:harvest) { FactoryGirl.create(:harvest) } + let(:photo) { FactoryGirl.create(:photo) } + + before do + harvest.photos << photo end it 'has a photo' do - @harvest.photos.first.should eq @photo + harvest.photos.first.should eq photo end it 'deletes association with photos when photo is deleted' do - @photo.destroy - @harvest.reload - @harvest.photos.should be_empty + photo.destroy + harvest.reload + harvest.photos.should be_empty end it 'has a default photo' do - @harvest.default_photo.should eq @photo + harvest.default_photo.should eq photo end it 'chooses the most recent photo' do @photo2 = FactoryGirl.create(:photo) - @harvest.photos << @photo2 - @harvest.default_photo.should eq @photo2 + harvest.photos << @photo2 + harvest.default_photo.should eq @photo2 end end end diff --git a/spec/models/member_spec.rb b/spec/models/member_spec.rb index 9b0481298..b91584953 100644 --- a/spec/models/member_spec.rb +++ b/spec/models/member_spec.rb @@ -3,105 +3,103 @@ require 'rails_helper' describe 'member' do context 'valid member' do - before(:each) do - @member = FactoryGirl.create(:member) - end + + let(:member) { FactoryGirl.create(:member) } it 'should be fetchable from the database' do - @member2 = Member.find(@member.id) + @member2 = Member.find(member.id) @member2.should be_an_instance_of Member @member2.login_name.should match(/member\d+/) @member2.encrypted_password.should_not be_nil end it 'should have a friendly slug' do - @member.slug.should match(/member\d+/) + member.slug.should match(/member\d+/) end it 'has a bio' do - @member.bio = 'I love seeds' - @member.bio.should eq 'I love seeds' + member.bio = 'I love seeds' + member.bio.should eq 'I love seeds' end it 'should have a default garden' do - @member.gardens.count.should == 1 + member.gardens.count.should == 1 end it 'should have a accounts entry' do - @member.account.should be_an_instance_of Account + member.account.should be_an_instance_of Account end it "should have a default-type account by default" do - @member.account.account_type.name.should eq Growstuff::Application.config.default_account_type - @member.is_paid?.should be(false) + member.account.account_type.name.should eq Growstuff::Application.config.default_account_type + member.is_paid?.should be(false) end it "doesn't show email by default" do - @member.show_email.should be(false) + member.show_email.should be(false) end it 'should stringify as the login_name' do - @member.to_s.should match(/member\d+/) - "#{@member}".should match(/member\d+/) + member.to_s.should match(/member\d+/) + "#{member}".should match(/member\d+/) end it 'should be able to fetch posts' do - @post = FactoryGirl.create(:post, :author => @member) - @member.posts.should eq [@post] + @post = FactoryGirl.create(:post, :author => member) + member.posts.should eq [@post] end it 'should be able to fetch gardens' do - @member.gardens.first.name.should eq "Garden" + member.gardens.first.name.should eq "Garden" end it 'has many plantings' do - @planting = FactoryGirl.create(:planting, :owner => @member) - @member.plantings.size.should eq 1 + @planting = FactoryGirl.create(:planting, :owner => member) + member.plantings.size.should eq 1 end it "has many comments" do - @comment1 = FactoryGirl.create(:comment, :author => @member) - @comment2 = FactoryGirl.create(:comment, :author => @member) - @member.comments.length.should == 2 + @comment1 = FactoryGirl.create(:comment, :author => member) + @comment2 = FactoryGirl.create(:comment, :author => member) + member.comments.length.should == 2 end it "has many forums" do - @forum1 = FactoryGirl.create(:forum, :owner => @member) - @forum2 = FactoryGirl.create(:forum, :owner => @member) - @member.forums.length.should == 2 + @forum1 = FactoryGirl.create(:forum, :owner => member) + @forum2 = FactoryGirl.create(:forum, :owner => member) + member.forums.length.should == 2 end it 'has location and lat/long fields' do - @member.update_attributes(:location => 'Greenwich, UK') - @member.location.should eq 'Greenwich, UK' - @member.latitude.round(2).should eq 51.48 - @member.longitude.round(2).should eq 0.00 + member.update_attributes(:location => 'Greenwich, UK') + member.location.should eq 'Greenwich, UK' + member.latitude.round(2).should eq 51.48 + member.longitude.round(2).should eq 0.00 end it 'empties the lat/long if location removed' do - @member.update_attributes(:location => 'Greenwich, UK') - @member.update_attributes(:location => '') - @member.location.should eq '' - @member.latitude.should be_nil - @member.longitude.should be_nil + member.update_attributes(:location => 'Greenwich, UK') + member.update_attributes(:location => '') + member.location.should eq '' + member.latitude.should be_nil + member.longitude.should be_nil end it 'fails gracefully for unfound locations' do - @member.update_attributes(:location => 'Tatooine') - @member.location.should eq 'Tatooine' - @member.latitude.should be_nil - @member.longitude.should be_nil + member.update_attributes(:location => 'Tatooine') + member.location.should eq 'Tatooine' + member.latitude.should be_nil + member.longitude.should be_nil end end context 'no TOS agreement' do - before(:each) do - @member = FactoryGirl.build(:no_tos_member) - end + + let(:member) { FactoryGirl.build(:no_tos_member) } it "should refuse to save a member who hasn't agreed to the TOS" do - @member.save.should_not be(true) + member.save.should_not be(true) end end @@ -190,15 +188,17 @@ describe 'member' do end context 'roles' do - before(:each) do - @member = FactoryGirl.create(:member) - @role = FactoryGirl.create(:role) - @member.roles << @role + + let(:member) { FactoryGirl.create(:member) } + let(:role) { FactoryGirl.create(:role) } + + before do + member.roles << role end it 'has a role' do - @member.roles.first.should eq @role - @member.has_role?(:moderator).should eq true + member.roles.first.should eq role + member.has_role?(:moderator).should eq true end it 'sets up roles in factories' do @@ -209,8 +209,8 @@ describe 'member' do it 'converts role names properly' do # need to make sure spaces get turned to underscores @role = FactoryGirl.create(:role, :name => "a b c") - @member.roles << @role - @member.has_role?(:a_b_c).should eq true + member.roles << @role + member.has_role?(:a_b_c).should eq true end end @@ -303,73 +303,71 @@ describe 'member' do end context "paid accounts" do - before(:each) do - @member = FactoryGirl.create(:member) - end + + let(:member) { FactoryGirl.create(:member) } it "recognises a permanent paid account" do @account_type = FactoryGirl.create(:account_type, :is_paid => true, :is_permanent_paid => true) - @member.account.account_type = @account_type - @member.is_paid?.should be(true) + member.account.account_type = @account_type + member.is_paid?.should be(true) end it "recognises a current paid account" do @account_type = FactoryGirl.create(:account_type, :is_paid => true, :is_permanent_paid => false) - @member.account.account_type = @account_type - @member.account.paid_until = Time.zone.now + 1.month - @member.is_paid?.should be(true) + member.account.account_type = @account_type + member.account.paid_until = Time.zone.now + 1.month + member.is_paid?.should be(true) end it "recognises an expired paid account" do @account_type = FactoryGirl.create(:account_type, :is_paid => true, :is_permanent_paid => false) - @member.account.account_type = @account_type - @member.account.paid_until = Time.zone.now - 1.minute - @member.is_paid?.should be(false) + member.account.account_type = @account_type + member.account.paid_until = Time.zone.now - 1.minute + member.is_paid?.should be(false) end it "recognises a free account" do @account_type = FactoryGirl.create(:account_type, :is_paid => false, :is_permanent_paid => false) - @member.account.account_type = @account_type - @member.is_paid?.should be(false) + member.account.account_type = @account_type + member.is_paid?.should be(false) end it "recognises a free account even with paid_until set" do @account_type = FactoryGirl.create(:account_type, :is_paid => false, :is_permanent_paid => false) - @member.account.account_type = @account_type - @member.account.paid_until = Time.zone.now + 1.month - @member.is_paid?.should be(false) + member.account.account_type = @account_type + member.account.paid_until = Time.zone.now + 1.month + member.is_paid?.should be(false) end end context "update account" do - before(:each) do - @product = FactoryGirl.create(:product, - :paid_months => 3 - ) - @member = FactoryGirl.create(:member) - end + + let(:product) { FactoryGirl.create(:product, + :paid_months => 3 + )} + let(:member) { FactoryGirl.create(:member) } it "sets account_type" do - @member.update_account_after_purchase(@product) - @member.account.account_type.should eq @product.account_type + member.update_account_after_purchase(product) + member.account.account_type.should eq product.account_type end it "sets paid_until" do - @member.account.paid_until = nil # blank for now, as if never paid before - @member.update_account_after_purchase(@product) + member.account.paid_until = nil # blank for now, as if never paid before + member.update_account_after_purchase(product) # stringify to avoid millisecond problems... - @member.account.paid_until.to_s.should eq (Time.zone.now + 3.months).to_s + member.account.paid_until.to_s.should eq (Time.zone.now + 3.months).to_s # and again to make sure it works for currently paid accounts - @member.update_account_after_purchase(@product) - @member.account.paid_until.to_s.should eq (Time.zone.now + 3.months + 3.months).to_s + member.update_account_after_purchase(product) + member.account.paid_until.to_s.should eq (Time.zone.now + 3.months + 3.months).to_s end end @@ -382,30 +380,33 @@ describe 'member' do end context 'member who followed another member' do - before(:each) do - @member1 = FactoryGirl.create(:member) - @member2 = FactoryGirl.create(:member) - @member3 = FactoryGirl.create(:member) - @follow = @member1.follows.create(:follower_id => @member1.id, :followed_id => @member2.id) + + + let(:member1) { FactoryGirl.create(:member) } + let(:member2) { FactoryGirl.create(:member) } + let(:member3) { FactoryGirl.create(:member) } + + before do + @follow = member1.follows.create(:follower_id => member1.id, :followed_id => member2.id) end context 'already_following' do it 'detects that member is already following a member' do - expect(@member1.already_following?(@member2)).to eq true + expect(member1.already_following?(member2)).to eq true end it 'detects that member is not already following a member' do - expect(@member1.already_following?(@member3)).to eq false + expect(member1.already_following?(member3)).to eq false end end context 'get_follow' do it 'gets the correct follow for a followed member' do - expect(@member1.get_follow(@member2).id).to eq @follow.id + expect(member1.get_follow(member2).id).to eq @follow.id end it 'returns nil for a member that is not followed' do - expect(@member1.get_follow(@member3)).to be_nil + expect(member1.get_follow(member3)).to be_nil end end diff --git a/spec/models/notification_spec.rb b/spec/models/notification_spec.rb index 27924ebed..d14f731d1 100644 --- a/spec/models/notification_spec.rb +++ b/spec/models/notification_spec.rb @@ -1,32 +1,31 @@ require 'rails_helper' describe Notification do - before(:each) do - @notification = FactoryGirl.create(:notification) - end + + let(:notification) { FactoryGirl.create(:notification) } it "belongs to a post" do - @notification.post.should be_an_instance_of Post + notification.post.should be_an_instance_of Post end it "belongs to a recipient" do - @notification.recipient.should be_an_instance_of Member + notification.recipient.should be_an_instance_of Member end it "belongs to a sender" do - @notification.sender.should be_an_instance_of Member + notification.sender.should be_an_instance_of Member end it "has a scope for unread" do - Notification.unread.should eq [@notification] + Notification.unread.should eq [notification] @n2 = FactoryGirl.create(:notification, :read => true) - Notification.unread.should eq [@notification] + Notification.unread.should eq [notification] @n3 = FactoryGirl.create(:notification, :read => false) - Notification.unread.should eq [@n3, @notification] + Notification.unread.should eq [@n3, notification] end it "counts unread" do - @who = @notification.recipient + @who = notification.recipient @n2 = FactoryGirl.create(:notification, :recipient => @who, :read => false) @who.notifications.unread_count.should eq 2 end @@ -38,9 +37,9 @@ describe Notification do end it "doesn't send email to people who don't want it" do - @notification = FactoryGirl.create(:no_email_notification) - @notification.send_email - ActionMailer::Base.deliveries.last.to.should_not == [@notification.recipient.email] + notification = FactoryGirl.create(:no_email_notification) + notification.send_email + ActionMailer::Base.deliveries.last.to.should_not == [notification.recipient.email] end it "sends email on creation" do @@ -49,13 +48,13 @@ describe Notification do end it "replaces missing subjects with (no subject)" do - @notification = FactoryGirl.create(:notification, :subject => nil) - @notification.subject.should == "(no subject)" + notification = FactoryGirl.create(:notification, :subject => nil) + notification.subject.should == "(no subject)" end it "replaces whitespace-only subjects with (no subject)" do - @notification = FactoryGirl.create(:notification, :subject => " ") - @notification.subject.should == "(no subject)" + notification = FactoryGirl.create(:notification, :subject => " ") + notification.subject.should == "(no subject)" end end diff --git a/spec/models/order_item_spec.rb b/spec/models/order_item_spec.rb index 20c0be94f..bd3b8f195 100644 --- a/spec/models/order_item_spec.rb +++ b/spec/models/order_item_spec.rb @@ -1,27 +1,26 @@ require 'rails_helper' describe OrderItem do - before(:each) do - @order_item = FactoryGirl.create(:order_item) - end + + let(:order_item) { FactoryGirl.create(:order_item) } it "has an order and a product" do - @order_item.order.should be_an_instance_of Order - @order_item.product.should be_an_instance_of Product + order_item.order.should be_an_instance_of Order + order_item.product.should be_an_instance_of Product end it "validates price > product.min_price" do @product = FactoryGirl.create(:product) - @order_item = FactoryGirl.build(:order_item, :price => @product.min_price - 1) - @order_item.should_not be_valid + order_item = FactoryGirl.build(:order_item, :price => @product.min_price - 1) + order_item.should_not be_valid end it "doesn't let you add two items to an order" do @product = FactoryGirl.create(:product) @order = FactoryGirl.create(:order) - @order_item = FactoryGirl.build(:order_item, :order => @order) - @order_item.should be_valid - @order_item.save + order_item = FactoryGirl.build(:order_item, :order => @order) + order_item.should be_valid + order_item.save @order_item2 = FactoryGirl.build(:order_item, :order => @order) @order_item2.should_not be_valid end diff --git a/spec/models/planting_spec.rb b/spec/models/planting_spec.rb index fba4fabe1..16bb8b613 100644 --- a/spec/models/planting_spec.rb +++ b/spec/models/planting_spec.rb @@ -2,26 +2,24 @@ require 'rails_helper' describe Planting do - before(:each) do - @crop = FactoryGirl.create(:tomato) - @garden_owner = FactoryGirl.create(:member) - @garden = FactoryGirl.create(:garden, :owner => @garden_owner) - @planting = FactoryGirl.create(:planting, - :crop => @crop, :garden => @garden) - end + let(:crop) { FactoryGirl.create(:tomato) } + let(:garden_owner) { FactoryGirl.create(:member) } + let(:garden) { FactoryGirl.create(:garden, :owner => garden_owner) } + let(:planting) { FactoryGirl.create(:planting, + :crop => crop, :garden => garden)} it 'has an owner' do - @planting.owner.should be_an_instance_of Member + planting.owner.should be_an_instance_of Member end it "owner isn't necessarily the garden owner" do # a new owner should be created automatically by FactoryGirl # note that formerly, the planting belonged to an owner through the garden - @planting.owner.should_not eq @garden_owner + planting.owner.should_not eq garden_owner end it "generates a location" do - @planting.location.should eq "#{@garden_owner.login_name}'s #{@garden.name}" + planting.location.should eq "#{garden_owner.login_name}'s #{garden.name}" end it "sorts plantings in descending order of creation" do @@ -31,7 +29,7 @@ describe Planting do end it "should have a slug" do - @planting.slug.should match /^member\d+-springfield-community-garden-tomato$/ + planting.slug.should match /^member\d+-springfield-community-garden-tomato$/ end it 'should sort in reverse creation order' do @@ -41,16 +39,16 @@ describe Planting do context 'delegation' do it 'system name' do - @planting.crop_name.should eq @planting.crop.name + planting.crop_name.should eq planting.crop.name end it 'wikipedia url' do - @planting.crop_en_wikipedia_url.should eq @planting.crop.en_wikipedia_url + planting.crop_en_wikipedia_url.should eq planting.crop.en_wikipedia_url end it 'default scientific name' do - @planting.crop_default_scientific_name.should eq @planting.crop.default_scientific_name + planting.crop_default_scientific_name.should eq planting.crop.default_scientific_name end it 'plantings count' do - @planting.crop_plantings_count.should eq @planting.crop.plantings_count + planting.crop_plantings_count.should eq planting.crop.plantings_count end end @@ -79,12 +77,11 @@ describe Planting do end context 'sunniness' do - before(:each) do - @planting = FactoryGirl.create(:sunny_planting) - end + + let(:planting) { FactoryGirl.create(:sunny_planting) } it 'should have a sunniness value' do - @planting.sunniness.should eq 'sun' + planting.sunniness.should eq 'sun' end it 'all three valid sunniness values should work' do @@ -126,30 +123,32 @@ describe Planting do # we decided that all the tests for the planting/photo association would # be done on this side, not on the photos side context 'photos' do - before(:each) do - @planting = FactoryGirl.create(:planting) - @photo = FactoryGirl.create(:photo) - @planting.photos << @photo + + let(:planting) { FactoryGirl.create(:planting) } + let(:photo) { FactoryGirl.create(:photo) } + + before do + planting.photos << photo end it 'has a photo' do - @planting.photos.first.should eq @photo + planting.photos.first.should eq photo end it 'deletes association with photos when photo is deleted' do - @photo.destroy - @planting.reload - @planting.photos.should be_empty + photo.destroy + planting.reload + planting.photos.should be_empty end it 'has a default photo' do - @planting.default_photo.should eq @photo + planting.default_photo.should eq photo end it 'chooses the most recent photo' do @photo2 = FactoryGirl.create(:photo) - @planting.photos << @photo2 - @planting.default_photo.should eq @photo2 + planting.photos << @photo2 + planting.default_photo.should eq @photo2 end end diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index adce855d2..cb9324d3f 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -1,43 +1,42 @@ require 'rails_helper' describe Post do - before(:each) do - @member = FactoryGirl.create(:member) - end + + let(:member) { FactoryGirl.create(:member) } it "should be sorted in reverse order" do FactoryGirl.create(:post, :subject => 'first entry', - :author => @member, + :author => member, :created_at => 2.days.ago ) FactoryGirl.create(:post, :subject => 'second entry', - :author => @member, + :author => member, :created_at => 1.day.ago ) Post.first.subject.should == "second entry" end it "should have a slug" do - @post = FactoryGirl.create(:post, :author => @member) + @post = FactoryGirl.create(:post, :author => member) @time = @post.created_at @datestr = @time.strftime("%Y%m%d") # 2 digit day and month, full-length years # Counting digits using Math.log is not precise enough! @datestr.length.should == 4 + @time.year.to_s.size - @post.slug.should == "#{@member.login_name}-#{@datestr}-a-post" + @post.slug.should == "#{member.login_name}-#{@datestr}-a-post" end it "has many comments" do - @post = FactoryGirl.create(:post, :author => @member) + @post = FactoryGirl.create(:post, :author => member) @comment1 = FactoryGirl.create(:comment, :post => @post) @comment2 = FactoryGirl.create(:comment, :post => @post) @post.comments.length.should == 2 end it "destroys comments when deleted" do - @post = FactoryGirl.create(:post, :author => @member) + @post = FactoryGirl.create(:post, :author => member) @comment1 = FactoryGirl.create(:comment, :post => @post) @comment2 = FactoryGirl.create(:comment, :post => @post) @post.comments.length.should == 2 @@ -67,19 +66,21 @@ describe Post do end context "recent activity" do - before(:each) do + + before do Time.stub(:now => Time.now) - @post = FactoryGirl.create(:post, :created_at => 1.day.ago) end + + let(:post) { FactoryGirl.create(:post, :created_at => 1.day.ago) } it "sets recent activity to post time" do - @post.recent_activity.to_i.should eq @post.created_at.to_i + post.recent_activity.to_i.should eq post.created_at.to_i end it "sets recent activity to comment time" do - @comment = FactoryGirl.create(:comment, :post => @post, + @comment = FactoryGirl.create(:comment, :post => post, :created_at => 1.hour.ago) - @post.recent_activity.to_i.should eq @comment.created_at.to_i + post.recent_activity.to_i.should eq @comment.created_at.to_i end it "shiny new post is recently active" do @@ -90,8 +91,8 @@ describe Post do it "new comment on old post is recently active" do # now comment on an older post - @comment2 = FactoryGirl.create(:comment, :post => @post, :created_at => 1.second.ago) - Post.recently_active.first.should eq @post + @comment2 = FactoryGirl.create(:comment, :post => post, :created_at => 1.second.ago) + Post.recently_active.first.should eq post end end diff --git a/spec/models/scientific_name_spec.rb b/spec/models/scientific_name_spec.rb index 020f1cc0d..e506acccf 100644 --- a/spec/models/scientific_name_spec.rb +++ b/spec/models/scientific_name_spec.rb @@ -3,30 +3,28 @@ require 'rails_helper' describe ScientificName do context 'all fields present' do - before(:each) do - @sn = FactoryGirl.create(:zea_mays) - end + let(:sn) { FactoryGirl.create(:zea_mays) } it 'should save a basic scientific name' do - @sn.save.should be(true) + sn.save.should be(true) end it 'should be fetchable from the database' do - @sn.save + sn.save @sn2 = ScientificName.find_by_scientific_name('Zea mays') @sn2.crop.name.should == 'maize' end it 'has a creator' do - @sn.save - @sn.creator.should be_an_instance_of Member + sn.save + sn.creator.should be_an_instance_of Member end end context 'invalid data' do it 'should not save a scientific name without a name' do - @sn = ScientificName.new - expect { @sn.save }.to raise_error ActiveRecord::StatementInvalid + sn = ScientificName.new + expect { sn.save }.to raise_error ActiveRecord::StatementInvalid end end end diff --git a/spec/models/seed_spec.rb b/spec/models/seed_spec.rb index 747dbee71..58ae56ea7 100644 --- a/spec/models/seed_spec.rb +++ b/spec/models/seed_spec.rb @@ -2,17 +2,15 @@ require 'rails_helper' describe Seed do - before(:each) do - @seed = FactoryGirl.build(:seed) - end + let(:seed) { FactoryGirl.build(:seed) } it 'should save a basic seed' do - @seed.save.should be(true) + seed.save.should be(true) end it "should have a slug" do - @seed.save - @seed.slug.should match(/member\d+-magic-bean/) + seed.save + seed.slug.should match(/member\d+-magic-bean/) end context 'quantity' do