refactor tests to use let

instead of before blocks creating instance variables
pronto checked

#6192
This commit is contained in:
zaziemo 2015-07-15 13:10:54 +02:00 committed by Dennis Schubert
parent 8823bb01a2
commit c5a2334fbe
8 changed files with 84 additions and 111 deletions

View file

@ -5,27 +5,27 @@
require 'spec_helper' require 'spec_helper'
describe AccountDeletion, :type => :model do describe AccountDeletion, :type => :model do
let(:ad_new) { AccountDeletion.new(person: alice.person) } let(:account_deletion_new) { AccountDeletion.new(person: alice.person) }
let(:ad_create) { AccountDeletion.create(person: alice.person) } let(:account_deletion_create) { AccountDeletion.create(person: alice.person) }
it 'assigns the diaspora_handle from the person object' do it 'assigns the diaspora_handle from the person object' do
expect(ad_new.diaspora_handle).to eq(alice.person.diaspora_handle) expect(account_deletion_new.diaspora_handle).to eq(alice.person.diaspora_handle)
end end
it 'fires a job after creation'do it 'fires a job after creation'do
expect(Workers::DeleteAccount).to receive(:perform_async).with(anything) expect(Workers::DeleteAccount).to receive(:perform_async).with(anything)
ad_create account_deletion_create
end end
describe "#perform!" do describe "#perform!" do
it 'creates a deleter' do it 'creates a deleter' do
expect(AccountDeleter).to receive(:new).with(alice.person.diaspora_handle).and_return(double(perform!: true)) expect(AccountDeleter).to receive(:new).with(alice.person.diaspora_handle).and_return(double(perform!: true))
ad_new.perform! account_deletion_new.perform!
end end
it 'dispatches the account deletion if the user exists' do it 'dispatches the account deletion if the user exists' do
expect(ad_new).to receive(:dispatch) expect(account_deletion_new).to receive(:dispatch)
ad_new.perform! account_deletion_new.perform!
end end
it 'does not dispatch an account deletion for non-local people' do it 'does not dispatch an account deletion for non-local people' do
@ -35,15 +35,15 @@ describe AccountDeletion, :type => :model do
end end
it 'marks an AccountDeletion as completed when successful' do it 'marks an AccountDeletion as completed when successful' do
ad_create.perform! account_deletion_create.perform!
expect(ad_create.reload.completed_at).not_to be_nil expect(account_deletion_create.reload.completed_at).not_to be_nil
end end
end end
describe '#dispatch' do describe '#dispatch' do
it 'creates a public postzord' do it 'creates a public postzord' do
expect(Postzord::Dispatcher::Public).to receive(:new).and_return(double.as_null_object) expect(Postzord::Dispatcher::Public).to receive(:new).and_return(double.as_null_object)
ad_new.dispatch account_deletion_new.dispatch
end end
end end
@ -51,20 +51,20 @@ describe AccountDeletion, :type => :model do
it 'includes all remote contacts' do it 'includes all remote contacts' do
alice.share_with(remote_raphael, alice.aspects.first) alice.share_with(remote_raphael, alice.aspects.first)
expect(ad_new.subscribers(alice)).to eq([remote_raphael]) expect(account_deletion_new.subscribers(alice)).to eq([remote_raphael])
end end
it 'includes remote resharers' do it 'includes remote resharers' do
sm = FactoryGirl.create( :status_message, public: true, author: alice.person) status_message = FactoryGirl.create(:status_message, public: true, author: alice.person)
FactoryGirl.create( :reshare, author: remote_raphael, root: sm) FactoryGirl.create(:reshare, author: remote_raphael, root: status_message)
FactoryGirl.create( :reshare, author: local_luke.person, root: sm) FactoryGirl.create(:reshare, author: local_luke.person, root: status_message)
expect(ad_new.subscribers(alice)).to eq([remote_raphael]) expect(account_deletion_new.subscribers(alice)).to eq([remote_raphael])
end end
end end
describe 'serialization' do describe 'serialization' do
let(:xml) { ad_new.to_xml.to_s } let(:xml) { account_deletion_new.to_xml.to_s }
it 'should have a diaspora_handle' do it 'should have a diaspora_handle' do
expect(xml.include?(alice.person.diaspora_handle)).to eq(true) expect(xml.include?(alice.person.diaspora_handle)).to eq(true)

View file

@ -22,16 +22,16 @@ describe ActsAsTaggableOn::Tag, :type => :model do
it "removes punctuation and whitespace" do it "removes punctuation and whitespace" do
{ {
'node.js' => 'nodejs', "node.js" => "nodejs",
'.dotatstart' => 'dotatstart', ".dotatstart" => "dotatstart",
'you,inside' => 'youinside', "you,inside" => "youinside",
'iam(parenthetical)' => 'iamparenthetical', "iam(parenthetical)" => "iamparenthetical",
'imeanit?maybe' => 'imeanitmaybe', "imeanit?maybe" => "imeanitmaybe",
'imeanit!' => 'imeanit', "imeanit!" => "imeanit",
'how about spaces' => 'howaboutspaces', "how about spaces" => "howaboutspaces",
"other\twhitespace\n" => 'otherwhitespace', "other\twhitespace\n" => "otherwhitespace",
'hash#inside' => 'hashinside', "hash#inside" => "hashinside",
'f!u@n#k$y%-<c>^h&a*r(a)c{t}e[r]s' => 'funky-characters' "f!u@n#k$y%-<c>^h&a*r(a)c{t}e[r]s" => "funky-characters"
}.each do |invalid, normalized| }.each do |invalid, normalized|
expect(tag.normalize(invalid)).to eq(normalized) expect(tag.normalize(invalid)).to eq(normalized)
end end

View file

@ -5,28 +5,26 @@
require 'spec_helper' require 'spec_helper'
describe AspectMembership, :type => :model do describe AspectMembership, :type => :model do
describe '#before_destroy' do describe '#before_destroy' do
let(:aspect) { alice.aspects.create(name: "two") } let(:aspect) { alice.aspects.create(name: "two") }
let(:contact) { alice.contact_for(bob.person) } let(:contact) { alice.contact_for(bob.person) }
let(:am) { alice.aspects.where(name: "generic").first.aspect_memberships.first } let(:aspect_membership) { alice.aspects.where(name: "generic").first.aspect_memberships.first }
before do before do
allow(am).to receive(:user).and_return(alice) allow(aspect_membership).to receive(:user).and_return(alice)
end end
it 'calls disconnect if its the last aspect for the contact' do it 'calls disconnect if its the last aspect for the contact' do
expect(alice).to receive(:disconnect).with(contact) expect(alice).to receive(:disconnect).with(contact)
am.destroy aspect_membership.destroy
end end
it 'does not call disconnect if its not the last aspect for the contact' do it 'does not call disconnect if its not the last aspect for the contact' do
expect(alice).not_to receive(:disconnect) expect(alice).not_to receive(:disconnect)
alice.add_contact_to_aspect(contact, aspect) alice.add_contact_to_aspect(contact, aspect)
am.destroy aspect_membership.destroy
end end
end end
end end

View file

@ -22,7 +22,7 @@ describe Aspect, :type => :model do
end end
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

View file

@ -6,9 +6,9 @@ require 'spec_helper'
require Rails.root.join("spec", "shared_behaviors", "relayable") require Rails.root.join("spec", "shared_behaviors", "relayable")
describe Comment, :type => :model do describe Comment, :type => :model do
let(:alices_aspect) { alice.aspects.first } let(:alices_aspect) { alice.aspects.first }
let(:status_bob) { bob.post(:status_message, text: "hello", to: bob.aspects.first.id) } let(:status_bob) { bob.post(:status_message, text: "hello", to: bob.aspects.first.id) }
let(:comment_alice) { alice.comment!(status_bob, "why so formal?") } let(:comment_alice) { alice.comment!(status_bob, "why so formal?") }
describe 'comment#notification_type' do describe 'comment#notification_type' do
it "returns 'comment_on_post' if the comment is on a post you own" do it "returns 'comment_on_post' if the comment is on a post you own" do
@ -70,11 +70,11 @@ describe Comment, :type => :model do
end end
describe 'xml' do describe 'xml' do
let(:commenter) { create(:user) } let(:commenter) { create(:user) }
let(:commenter_aspect) { commenter.aspects.create(name: "bruisers") } let(:commenter_aspect) { commenter.aspects.create(name: "bruisers") }
let(:post) { alice.post :status_message, text: "hello", to: alices_aspect.id } let(:post) { alice.post :status_message, text: "hello", to: alices_aspect.id }
let(:comment) { commenter.comment!(post, "Fool!") } let(:comment) { commenter.comment!(post, "Fool!") }
let(:xml) { comment.to_xml.to_s } let(:xml) { comment.to_xml.to_s }
before do before do
connect_users(alice, alices_aspect, commenter, commenter_aspect) connect_users(alice, alices_aspect, commenter, commenter_aspect)
@ -110,12 +110,12 @@ describe Comment, :type => :model do
end end
describe 'it is relayable' do describe 'it is relayable' do
let(:remote_parent) { build(:status_message, author: remote_raphael) } let(:remote_parent) { build(:status_message, author: remote_raphael) }
let(:local_parent) { local_luke.post :status_message, text: "hi", to: local_luke.aspects.first } let(:local_parent) { local_luke.post :status_message, text: "hi", to: local_luke.aspects.first }
let(:object_by_parent_author) { local_luke.comment!(local_parent, "yo!") } let(:object_by_parent_author) { local_luke.comment!(local_parent, "yo!") }
let(:object_by_recipient) { local_leia.build_comment(text: "yo", post: local_parent) } let(:object_by_recipient) { local_leia.build_comment(text: "yo", post: local_parent) }
let(:dup_object_by_parent_author) { object_by_parent_author.dup } let(:dup_object_by_parent_author) { object_by_parent_author.dup }
let(:object_on_remote_parent) { local_luke.comment!(remote_parent, "Yeah, it was great") } let(:object_on_remote_parent) { local_luke.comment!(remote_parent, "Yeah, it was great") }
before do before do
# shared_behaviors/relayable.rb is still using instance variables, so we need to define them here. # shared_behaviors/relayable.rb is still using instance variables, so we need to define them here.
@ -140,5 +140,4 @@ describe Comment, :type => :model do
end end
it_should_behave_like 'it is taggable' it_should_behave_like 'it is taggable'
end end
end end

View file

@ -7,7 +7,7 @@ require 'spec_helper'
describe Contact, :type => :model do describe Contact, :type => :model do
describe 'aspect_memberships' do describe 'aspect_memberships' do
it 'deletes dependent aspect memberships' do it 'deletes dependent aspect memberships' do
expect{ expect {
alice.contact_for(bob.person).destroy alice.contact_for(bob.person).destroy
}.to change(AspectMembership, :count).by(-1) }.to change(AspectMembership, :count).by(-1)
end end
@ -103,7 +103,7 @@ describe Contact, :type => :model do
describe '#contacts' do describe '#contacts' do
before do before do
bob.aspects.create(name: 'next') bob.aspects.create(name: "next")
bob.aspects(true) bob.aspects(true)
@original_aspect = bob.aspects.where(name: "generic").first @original_aspect = bob.aspects.where(name: "generic").first
@ -133,7 +133,7 @@ describe Contact, :type => :model do
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(&:id)).to match_array([eve.person].concat(@people1).map(&: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
@ -143,8 +143,8 @@ 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(&:id)
expect(contact_ids.uniq).to eq(contact_ids) expect(contact_ids.uniq).to eq(contact_ids)
end end
end end
@ -159,14 +159,9 @@ describe Contact, :type => :model do
end end
context 'requesting' do context 'requesting' do
let(:contact) { Contact.new } let(:contact) { Contact.new user: user, person: person }
let(:user) { build(:user) } let(:user) { build(:user) }
let(:person) { build(:person) } let(:person) { build(:person) }
before do
contact.user = user
contact.person = person
end
describe '#generate_request' do describe '#generate_request' do
it 'makes a request' do it 'makes a request' do

View file

@ -5,23 +5,19 @@
require 'spec_helper' require 'spec_helper'
describe Conversation, :type => :model do 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) do let(:create_hash) {{author: user1.person, participant_ids: participant_ids,
{ subject: "cool stuff", messages_attributes: [{author: user1.person, text: "hey"}]}}
author: user1.person, let(:conversation) { Conversation.create(create_hash) }
participant_ids: participant_ids, let(:message_last) { Message.create(author: user2.person, created_at: Time.now + 100,
subject: "cool stuff", text: "last", conversation_id: conversation.id) }
messages_attributes: [ {author: user1.person, text: "hey"} ] let(:message_first) { Message.create(author: user2.person, created_at: Time.now + 100,
} text: "first", conversation_id: conversation.id) }
end
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_first) { Message.create(author: user2.person, created_at: Time.now + 100, text: "first", conversation_id: conversation.id) }
it 'creates a message on create' do it 'creates a message on create' do
expect{ conversation }.to change(Message, :count).by(1) expect { conversation }.to change(Message, :count).by(1)
end end
describe '#last_author' do describe '#last_author' do
@ -49,13 +45,12 @@ describe Conversation, :type => :model do
end end
it 'returns nil if there are no unread messages in a conversation' do it 'returns nil if there are no unread messages in a conversation' do
conversation.conversation_visibilities.where(person_id: user1.person.id).first.tap { |cv| cv.unread = 0 }.save conversation.conversation_visibilities.where(person_id: user1.person.id).first.tap {|cv| cv.unread = 0 }.save
expect(conversation.first_unread_message(user1)).to be_nil expect(conversation.first_unread_message(user1)).to be_nil
end end
end end
describe '#set_read' do describe '#set_read' do
before do before do
conversation conversation
message_first.increase_unread(user1) message_first.increase_unread(user1)
@ -71,7 +66,7 @@ describe Conversation, :type => :model do
context 'transport' do context 'transport' do
let(:conversation_message) { conversation.messages.first } let(:conversation_message) { conversation.messages.first }
let(:xml) { conversation.to_diaspora_xml } let(:xml) { conversation.to_diaspora_xml }
before do before do
conversation conversation
@ -79,13 +74,13 @@ describe Conversation, :type => :model do
describe 'serialization' do describe 'serialization' do
it 'serializes the message' do it 'serializes the message' do
expect(xml.gsub(/\s/, '')).to include(conversation_message.to_xml.to_s.gsub(/\s/, '')) expect(xml.gsub(/\s/, "")).to include(conversation_message.to_xml.to_s.gsub(/\s/, ""))
end end
it 'serializes the participants' do it 'serializes the participants' do
create_hash[:participant_ids].each{ |id| create_hash[:participant_ids].each do |id|
expect(xml).to include(Person.find(id).diaspora_handle) expect(xml).to include(Person.find(id).diaspora_handle)
} end
end end
it 'serializes the created_at time' do it 'serializes the created_at time' do
@ -95,7 +90,7 @@ describe Conversation, :type => :model do
describe '#subscribers' do describe '#subscribers' do
it 'returns the recipients for the post owner' do it 'returns the recipients for the post owner' do
expect(conversation.subscribers(user1)).to eq(user1.contacts.map{|c| c.person}) expect(conversation.subscribers(user1)).to eq(user1.contacts.map(&:person))
end end
end end
@ -106,17 +101,17 @@ describe Conversation, :type => :model do
end end
it 'creates a message' do it 'creates a message' do
expect{ expect {
Diaspora::Parser.from_xml(xml).receive(user1, user2.person) Diaspora::Parser.from_xml(xml).receive(user1, user2.person)
}.to change(Message, :count).by(1) }.to change(Message, :count).by(1)
end end
it 'creates a conversation' do it 'creates a conversation' do
expect{ expect {
Diaspora::Parser.from_xml(xml).receive(user1, user2.person) Diaspora::Parser.from_xml(xml).receive(user1, user2.person)
}.to change(Conversation, :count).by(1) }.to change(Conversation, :count).by(1)
end end
it 'creates appropriate visibilities' do it 'creates appropriate visibilities' do
expect{ expect {
Diaspora::Parser.from_xml(xml).receive(user1, user2.person) Diaspora::Parser.from_xml(xml).receive(user1, user2.person)
}.to change(ConversationVisibility, :count).by(participant_ids.size) }.to change(ConversationVisibility, :count).by(participant_ids.size)
end end
@ -132,13 +127,9 @@ describe Conversation, :type => :model do
describe "#invalid parameters" do describe "#invalid parameters" do
context "local author" do context "local author" do
let(:invalid_hash) do let(:invalid_hash) {{author: peter.person, participant_ids: [peter.person.id, user1.person.id],
{ 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
@ -146,16 +137,12 @@ describe Conversation, :type => :model do
end end
context "remote author" do context "remote author" 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) do let(:invalid_hash_remote) {{author: remote_person, participant_ids: participant_ids,
{ 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

View file

@ -5,17 +5,11 @@
require 'spec_helper' require 'spec_helper'
describe ConversationVisibility, type: :model do describe ConversationVisibility, type: :model do
let(:user1) { alice } let(:user1) { alice }
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) do let(:create_hash) {{author: user1.person, participant_ids: participant_ids, subject: "cool stuff",
{ messages_attributes: [{author: user1.person, text: "hey"}]}}
author: user1.person, let(:conversation) { Conversation.create(create_hash) }
participant_ids: participant_ids,
subject: 'cool stuff',
messages_attributes: [{ author: user1.person, text: 'hey' }]
}
end
let(:conversation) { Conversation.create(create_hash) }
it 'destroy conversation when no participant' do it 'destroy conversation when no participant' do
conversation.conversation_visibilities.each(&:destroy) conversation.conversation_visibilities.each(&:destroy)