refactor test: replacing instance variables in before blocks, renaming variables for easier understanding, simplify test set up with less different comments, correct two tests

This commit is contained in:
zaziemo 2015-07-13 16:52:46 +02:00 committed by Dennis Schubert
parent a9919fabd1
commit fccb5dae23

View file

@ -6,58 +6,57 @@ 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
before do
@alices_aspect = alice.aspects.first let(:alices_aspect) { alice.aspects.first }
@status = 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) }
end let(:comment_alice) { alice.comment!(status_bob, "why so formal?") }
describe 'comment#notification_type' do describe 'comment#notification_type' do
let (:comment) { alice.comment!(@status, "why so formal?") }
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
expect(comment.notification_type(bob, alice.person)).to eq(Notifications::CommentOnPost) expect(comment_alice.notification_type(bob, alice.person)).to eq(Notifications::CommentOnPost)
end end
it "returns 'also_commented' if the comment is on a post you participate to" do it "returns 'also_commented' if the comment is on a post you participate to" do
eve.participate! @status eve.participate! status_bob
expect(comment.notification_type(eve, alice.person)).to eq(Notifications::AlsoCommented) expect(comment_alice.notification_type(eve, alice.person)).to eq(Notifications::AlsoCommented)
end end
it 'returns false if the comment is not on a post you own and no one "also_commented"' do it 'returns false if the comment is not on a post you own and no one "also_commented"' do
comment = alice.comment!(@status, "I simply felt like issuing a greeting. Do step off.") expect(comment_alice.notification_type(eve, alice.person)).to be false
expect(comment.notification_type(eve, alice.person)).to be false
end end
context "also commented" do context "also commented" do
let(:comment_eve) { eve.comment!(status_bob, "I also commented on the first user's post") }
before do before do
alice.comment!(@status, "a-commenta commenta") comment_alice
@comment = eve.comment!(@status, "I also commented on the first user's post")
end end
it 'does not return also commented if the user commented' do it 'does not return also commented if the user commented' do
expect(@comment.notification_type(eve, alice.person)).to eq(false) expect(comment_eve.notification_type(eve, alice.person)).to eq(false)
end end
it "returns 'also_commented' if another person commented on a post you commented on" do it "returns 'also_commented' if another person commented on a post you commented on" do
expect(@comment.notification_type(alice, alice.person)).to eq(Notifications::AlsoCommented) expect(comment_eve.notification_type(alice, alice.person)).to eq(Notifications::AlsoCommented)
end end
end end
end end
describe 'User#comment' do describe 'User#comment' do
it "should be able to comment on one's own status" do it "should be able to comment on one's own status" do
alice.comment!(@status, "Yeah, it was great") bob.comment!(status_bob, "sup dog")
expect(@status.reload.comments.first.text).to eq("Yeah, it was great") expect(status_bob.reload.comments.first.text).to eq("sup dog")
end end
it "should be able to comment on a contact's status" do it "should be able to comment on a contact's status" do
bob.comment!(@status, "sup dog") comment_alice
expect(@status.reload.comments.first.text).to eq("sup dog") expect(status_bob.reload.comments.first.text).to eq("why so formal?")
end end
it 'does not multi-post a comment' do it 'does not multi-post a comment' do
expect { expect {
alice.comment!(@status, 'hello') comment_alice
}.to change { Comment.count }.by(1) }.to change { Comment.count }.by(1)
end end
end end
@ -65,19 +64,21 @@ describe Comment, :type => :model do
describe 'counter cache' do describe 'counter cache' do
it 'increments the counter cache on its post' do it 'increments the counter cache on its post' do
expect { expect {
alice.comment!(@status, "oh yeah") comment_alice
}.to change{ }.to change{
@status.reload.comments_count status_bob.reload.comments_count
}.by(1) }.by(1)
end end
end end
# hier weiter
describe 'xml' do describe 'xml' do
before do before do
@commenter = FactoryGirl.create(:user) @commenter = FactoryGirl.create(:user)
@commenter_aspect = @commenter.aspects.create(:name => "bruisers") @commenter_aspect = @commenter.aspects.create(:name => "bruisers")
connect_users(alice, @alices_aspect, @commenter, @commenter_aspect) connect_users(alice, alices_aspect, @commenter, @commenter_aspect)
@post = alice.post :status_message, :text => "hello", :to => @alices_aspect.id @post = alice.post :status_message, :text => "hello", :to => alices_aspect.id
@comment = @commenter.comment!(@post, "Fool!") @comment = @commenter.comment!(@post, "Fool!")
@xml = @comment.to_xml.to_s @xml = @comment.to_xml.to_s
end end
@ -125,7 +126,7 @@ describe Comment, :type => :model do
@object_on_remote_parent = @local_luke.comment!(@remote_parent, "Yeah, it was great") @object_on_remote_parent = @local_luke.comment!(@remote_parent, "Yeah, it was great")
end end
let(:build_object) { alice.build_comment(:post => @status, :text => "why so formal?") } let(:build_object) { alice.build_comment(:post => status_bob, :text => "why so formal?") }
it_should_behave_like 'it is relayable' it_should_behave_like 'it is relayable'
end end