From 3ff33d355f8565deb78170df885dc046ca22553f Mon Sep 17 00:00:00 2001 From: realtin Date: Tue, 14 Jul 2015 16:07:13 +0200 Subject: [PATCH] refactor test to use let --- spec/models/contact_spec.rb | 81 ++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 42 deletions(-) diff --git a/spec/models/contact_spec.rb b/spec/models/contact_spec.rb index 9f8370952..6bed1b92a 100644 --- a/spec/models/contact_spec.rb +++ b/spec/models/contact_spec.rb @@ -14,7 +14,7 @@ describe Contact, :type => :model do end context 'validations' do - let(:contact){Contact.new} + let(:contact) { Contact.new } it 'requires a user' do contact.valid? @@ -37,7 +37,7 @@ describe Contact, :type => :model do it 'validates uniqueness' do person = FactoryGirl.create(:person) - contact2 = alice.contacts.create(:person=>person) + contact2 = alice.contacts.create(person: person) expect(contact2).to be_valid contact.user = alice @@ -48,7 +48,7 @@ describe Contact, :type => :model do it "validates that the person's account is not closed" do person = FactoryGirl.create(:person, :closed_account => true) - contact = alice.contacts.new(:person=>person) + contact = alice.contacts.new(person: person) expect(contact).not_to be_valid expect(contact.errors.full_messages).to include "Cannot be in contact with a closed account" @@ -59,8 +59,8 @@ describe Contact, :type => :model do describe 'sharing' do it 'returns contacts with sharing true' do expect { - alice.contacts.create!(:sharing => true, :person => FactoryGirl.create(:person)) - alice.contacts.create!(:sharing => false, :person => FactoryGirl.create(:person)) + alice.contacts.create!(sharing: true, person: FactoryGirl.create(:person)) + alice.contacts.create!(sharing: false, person: FactoryGirl.create(:person)) }.to change{ Contact.sharing.count }.by(1) @@ -70,8 +70,8 @@ describe Contact, :type => :model do describe 'receiving' do it 'returns contacts with sharing true' do expect { - alice.contacts.create!(:receiving => true, :person => FactoryGirl.build(:person)) - alice.contacts.create!(:receiving => false, :person => FactoryGirl.build(:person)) + alice.contacts.create!(receiving: true, person: FactoryGirl.build(:person)) + alice.contacts.create!(receiving: false, person: FactoryGirl.build(:person)) }.to change{ Contact.receiving.count }.by(1) @@ -81,20 +81,20 @@ describe Contact, :type => :model do describe 'only_sharing' do it 'returns contacts with sharing true and receiving false' do expect { - alice.contacts.create!(:receiving => true, :sharing => true, :person => FactoryGirl.build(:person)) - alice.contacts.create!(:receiving => false, :sharing => true, :person => FactoryGirl.build(:person)) - alice.contacts.create!(:receiving => false, :sharing => true, :person => FactoryGirl.build(:person)) - alice.contacts.create!(:receiving => true, :sharing => false, :person => FactoryGirl.build(:person)) + alice.contacts.create!(receiving: true, sharing: true, person: FactoryGirl.build(:person)) + alice.contacts.create!(receiving: false, sharing: true, person: FactoryGirl.build(:person)) + alice.contacts.create!(receiving: false, sharing: true, person: FactoryGirl.build(:person)) + alice.contacts.create!(receiving: true, sharing: false, person: FactoryGirl.build(:person)) }.to change{ Contact.receiving.count }.by(2) end end - + describe "all_contacts_of_person" do it 'returns all contacts where the person is the passed in person' do person = FactoryGirl.create(:person) - contact1 = FactoryGirl.create(:contact, :person => person) + contact1 = FactoryGirl.create(:contact, person: person) contact2 = FactoryGirl.create(:contact) contacts = Contact.all_contacts_of_person(person) expect(contacts).to eq([contact1]) @@ -107,23 +107,23 @@ describe Contact, :type => :model do @alice = alice @bob = bob @eve = eve - @bob.aspects.create(:name => 'next') + @bob.aspects.create(name: 'next') @bob.aspects(true) - @original_aspect = @bob.aspects.where(:name => "generic").first - @new_aspect = @bob.aspects.where(:name => "next").first + @original_aspect = @bob.aspects.where(name: "generic").first + @new_aspect = @bob.aspects.where(name: "next").first @people1 = [] @people2 = [] 1.upto(5) do person = FactoryGirl.build(:person) - @bob.contacts.create(:person => person, :aspects => [@original_aspect]) + @bob.contacts.create(person: person, aspects: [@original_aspect]) @people1 << person end 1.upto(5) do person = FactoryGirl.build(:person) - @bob.contacts.create(:person => person, :aspects => [@new_aspect]) + @bob.contacts.create(person: person, aspects: [@new_aspect]) @people2 << person end #eve <-> bob <-> alice @@ -154,66 +154,63 @@ describe Contact, :type => :model do end context 'on a contact for a remote user' do - before do - @contact = @bob.contact_for @people1.first - end + let(:contact) { @bob.contact_for @people1.first } + it 'returns an empty array' do - expect(@contact.contacts).to eq([]) + expect(contact.contacts).to eq([]) end end end context 'requesting' do - before do - @contact = Contact.new - @user = FactoryGirl.build(:user) - @person = FactoryGirl.build(:person) + let(:contact) { Contact.new } + let(:user) { build(:user) } + let(:person) { build(:person) } - @contact.user = @user - @contact.person = @person + before do + contact.user = user + contact.person = person end describe '#generate_request' do it 'makes a request' do - allow(@contact).to receive(:user).and_return(@user) - request = @contact.generate_request + allow(contact).to receive(:user).and_return(user) + request = contact.generate_request - expect(request.sender).to eq(@user.person) - expect(request.recipient).to eq(@person) + expect(request.sender).to eq(user.person) + expect(request.recipient).to eq(person) end end describe '#dispatch_request' do it 'pushes to people' do - allow(@contact).to receive(:user).and_return(@user) + allow(contact).to receive(:user).and_return(user) m = double() expect(m).to receive(:post) expect(Postzord::Dispatcher).to receive(:build).and_return(m) - @contact.dispatch_request + contact.dispatch_request end end end describe "#not_blocked_user" do - before do - @contact = alice.contact_for(bob.person) - end + let(:contact) { alice.contact_for(bob.person) } it "is called on validate" do - expect(@contact).to receive(:not_blocked_user) - @contact.valid? + expect(contact).to receive(:not_blocked_user) + contact.valid? end it "adds to errors if potential contact is blocked by user" do person = eve.person - block = alice.blocks.create(:person => person) - bad_contact = alice.contacts.create(:person => person) + block = alice.blocks.create(person: person) + bad_contact = alice.contacts.create(person: person) expect(bad_contact.send(:not_blocked_user)).to be false end it "does not add to errors" do - expect(@contact.send(:not_blocked_user)).to be true + expect(contact.send(:not_blocked_user)).to be true end end end