refactor test to use let and better indentation

(#6192)
This commit is contained in:
realtin 2015-07-15 10:03:17 +02:00 committed by Dennis Schubert
parent 56e0d3d57d
commit 8823bb01a2
5 changed files with 61 additions and 52 deletions

View file

@ -8,7 +8,6 @@ describe ActsAsTaggableOn::Tag, :type => :model do
it 'downcases the tag name' do it 'downcases the tag name' do
expect(tag.autocomplete("CATS")).to eq([tag_cats]) expect(tag.autocomplete("CATS")).to eq([tag_cats])
end end
it 'does an end where on tags' do it 'does an end where on tags' do

View file

@ -23,8 +23,8 @@ describe Aspect, :type => :model do
it 'is able to have other users as contacts' do it 'is able to have other users as contacts' do
aspect = alice.aspects.create(name: 'losers') aspect = alice.aspects.create(name: 'losers')
Contact.create(user: alice, person: eve.person, aspects: [aspect]) Contact.create(user: alice, person: eve.person, aspects: [aspect])
expect(aspect.contacts.where(person_id: alice.person.id)).to be_empty expect(aspect.contacts.where(person_id: alice.person.id)).to be_empty
expect(aspect.contacts.where(person_id: eve.person.id)).not_to be_empty expect(aspect.contacts.where(person_id: eve.person.id)).not_to be_empty
expect(aspect.contacts.size).to eq(1) expect(aspect.contacts.size).to eq(1)

View file

@ -47,7 +47,6 @@ describe Contact, :type => :model do
it "validates that the person's account is not closed" do it "validates that the person's account is not closed" do
person = FactoryGirl.create(:person, :closed_account => true) 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).not_to be_valid
@ -104,26 +103,23 @@ describe Contact, :type => :model do
describe '#contacts' do describe '#contacts' do
before do before do
@alice = alice bob.aspects.create(name: 'next')
@bob = bob bob.aspects(true)
@eve = eve
@bob.aspects.create(name: 'next')
@bob.aspects(true)
@original_aspect = @bob.aspects.where(name: "generic").first @original_aspect = bob.aspects.where(name: "generic").first
@new_aspect = @bob.aspects.where(name: "next").first @new_aspect = bob.aspects.where(name: "next").first
@people1 = [] @people1 = []
@people2 = [] @people2 = []
1.upto(5) do 1.upto(5) do
person = FactoryGirl.build(:person) person = FactoryGirl.build(:person)
@bob.contacts.create(person: person, aspects: [@original_aspect]) bob.contacts.create(person: person, aspects: [@original_aspect])
@people1 << person @people1 << person
end end
1.upto(5) do 1.upto(5) do
person = FactoryGirl.build(:person) person = FactoryGirl.build(:person)
@bob.contacts.create(person: person, aspects: [@new_aspect]) bob.contacts.create(person: person, aspects: [@new_aspect])
@people2 << person @people2 << person
end end
#eve <-> bob <-> alice #eve <-> bob <-> alice
@ -131,13 +127,13 @@ describe Contact, :type => :model do
context 'on a contact for a local user' do context 'on a contact for a local user' do
before do before do
@alice.reload alice.reload
@alice.aspects.reload alice.aspects.reload
@contact = @alice.contact_for(@bob.person) @contact = alice.contact_for(bob.person)
end end
it "returns the target local user's contacts that are in the same aspect" do it "returns the target local user's contacts that are in the same aspect" do
expect(@contact.contacts.map{|p| p.id}).to match_array([@eve.person].concat(@people1).map{|p| p.id}) expect(@contact.contacts.map{|p| p.id}).to match_array([eve.person].concat(@people1).map{|p| p.id})
end end
it 'returns nothing if contacts_visible is false in that aspect' do it 'returns nothing if contacts_visible is false in that aspect' do
@ -147,14 +143,14 @@ describe Contact, :type => :model do
end end
it 'returns no duplicate contacts' do it 'returns no duplicate contacts' do
[@alice, @eve].each {|c| @bob.add_contact_to_aspect(@bob.contact_for(c.person), @bob.aspects.last)} [alice, eve].each {|c| bob.add_contact_to_aspect(bob.contact_for(c.person), bob.aspects.last)}
contact_ids = @contact.contacts.map{|p| p.id} contact_ids = @contact.contacts.map{|p| p.id}
expect(contact_ids.uniq).to eq(contact_ids) expect(contact_ids.uniq).to eq(contact_ids)
end end
end end
context 'on a contact for a remote user' do context 'on a contact for a remote user' do
let(:contact) { @bob.contact_for @people1.first } let(:contact) { bob.contact_for @people1.first }
it 'returns an empty array' do it 'returns an empty array' do
expect(contact.contacts).to eq([]) expect(contact.contacts).to eq([])
@ -203,7 +199,7 @@ describe Contact, :type => :model do
it "adds to errors if potential contact is blocked by user" do it "adds to errors if potential contact is blocked by user" do
person = eve.person person = eve.person
block = alice.blocks.create(person: person) alice.blocks.create(person: person)
bad_contact = alice.contacts.create(person: person) bad_contact = alice.contacts.create(person: person)
expect(bad_contact.send(:not_blocked_user)).to be false expect(bad_contact.send(:not_blocked_user)).to be false

View file

@ -8,8 +8,14 @@ describe Conversation, :type => :model do
let(:user1) { alice } let(:user1) { alice }
let(:user2) { bob } let(:user2) { bob }
let(:participant_ids) { [user1.contacts.first.person.id, user1.person.id] } let(:participant_ids) { [user1.contacts.first.person.id, user1.person.id] }
let(:create_hash) { {author: user1.person, participant_ids: participant_ids, subject: "cool stuff", let(:create_hash) do
messages_attributes: [ {author: user1.person, text: "hey"} ]} } {
author: user1.person,
participant_ids: participant_ids,
subject: "cool stuff",
messages_attributes: [ {author: user1.person, text: "hey"} ]
}
end
let(:conversation) { Conversation.create(create_hash) } let(:conversation) { Conversation.create(create_hash) }
let(:message_last) { Message.create(author: user2.person, created_at: Time.now + 100, text: "last", conversation_id: conversation.id) } let(:message_last) { Message.create(author: user2.person, created_at: Time.now + 100, text: "last", conversation_id: conversation.id) }
let(:message_first) { Message.create(author: user2.person, created_at: Time.now + 100, text: "first", conversation_id: conversation.id) } let(:message_first) { Message.create(author: user2.person, created_at: Time.now + 100, text: "first", conversation_id: conversation.id) }
@ -77,7 +83,7 @@ describe Conversation, :type => :model do
end end
it 'serializes the participants' do it 'serializes the participants' do
create_hash[:participant_ids].each{|id| create_hash[:participant_ids].each{ |id|
expect(xml).to include(Person.find(id).diaspora_handle) expect(xml).to include(Person.find(id).diaspora_handle)
} }
end end
@ -126,9 +132,13 @@ describe Conversation, :type => :model do
describe "#invalid parameters" do describe "#invalid parameters" do
context "local author" do context "local author" do
let(:invalid_hash) { {author: peter.person, participant_ids: [peter.person.id, user1.person.id], let(:invalid_hash) do
subject: "cool stuff", messages_attributes: [{author: peter.person, text: "hey"}]} } {
author: peter.person,
participant_ids: [peter.person.id, user1.person.id],
subject: "cool stuff", messages_attributes: [{author: peter.person, text: "hey"}]
}
end
it "is invalid with invalid recipient" do it "is invalid with invalid recipient" do
invalid_conversation = Conversation.create(invalid_hash) invalid_conversation = Conversation.create(invalid_hash)
expect(invalid_conversation).to be_invalid expect(invalid_conversation).to be_invalid
@ -139,9 +149,13 @@ describe Conversation, :type => :model do
let(:remote_person) { remote_raphael } let(:remote_person) { remote_raphael }
let(:local_user) { alice } let(:local_user) { alice }
let(:participant_ids) { [remote_person.id, local_user.person.id] } let(:participant_ids) { [remote_person.id, local_user.person.id] }
let(:invalid_hash_remote) { {author: remote_person, participant_ids: participant_ids, let(:invalid_hash_remote) do
subject: "cool stuff", messages_attributes: [{author: remote_person, text: "hey"}]} } {
author: remote_person,
participant_ids: participant_ids,
subject: "cool stuff", messages_attributes: [{author: remote_person, text: "hey"}]
}
end
it "is invalid with invalid recipient" do it "is invalid with invalid recipient" do
invalid_conversation_remote = Conversation.create(invalid_hash_remote) invalid_conversation_remote = Conversation.create(invalid_hash_remote)
expect(invalid_conversation_remote).to be_invalid expect(invalid_conversation_remote).to be_invalid