Transform Flag.flag! & unflag! into non-raising methods

They now return false and do nothing instead
This commit is contained in:
kikito
2015-08-31 19:02:50 +02:00
parent 358f8cc392
commit 27ece220f6
8 changed files with 30 additions and 44 deletions

View File

@@ -68,7 +68,7 @@ FactoryGirl.define do
trait :flagged do
after :create do |debate|
Flag.flag!(FactoryGirl.create(:user), debate)
Flag.flag(FactoryGirl.create(:user), debate)
end
end
end
@@ -101,7 +101,7 @@ FactoryGirl.define do
trait :flagged do
after :create do |debate|
Flag.flag!(FactoryGirl.create(:user), debate)
Flag.flag(FactoryGirl.create(:user), debate)
end
end
end

View File

@@ -155,7 +155,7 @@ feature 'Comments' do
user = create(:user)
debate = create(:debate)
comment = create(:comment, commentable: debate)
Flag.flag!(user, comment)
Flag.flag(user, comment)
login_as(user)
visit debate_path(debate)

View File

@@ -335,7 +335,7 @@ feature 'Debates' do
scenario "Unflagging", :js do
user = create(:user)
debate = create(:debate)
Flag.flag!(user, debate)
Flag.flag(user, debate)
login_as(user)
visit debate_path(debate)
@@ -432,9 +432,9 @@ feature 'Debates' do
expect(current_url).to include('tag=Deporte')
expect(page).to have_selector('#debates .debate', count: 2)
expect(page).to_not have_content(debate3.title)
expect(page).to have_content(debate1.title)
expect(page).to have_content(debate2.title)
expect(page).to_not have_content(debate3.title)
expect(page).to have_content(debate1.title)
expect(page).to have_content(debate2.title)
end
end
end

View File

@@ -52,14 +52,14 @@ describe Ability do
end
describe "already-flagged comments" do
before(:each) { Flag.flag!(user, comment) }
before(:each) { Flag.flag(user, comment) }
it { should_not be_able_to(:flag, comment) }
it { should be_able_to(:unflag, comment) }
end
describe "already-flagged debates" do
before(:each) { Flag.flag!(user, debate) }
before(:each) { Flag.flag(user, debate) }
it { should_not be_able_to(:flag, debate) }
it { should be_able_to(:unflag, debate) }

View File

@@ -5,41 +5,41 @@ describe Flag do
let(:user) { create(:user) }
let(:comment) { create(:comment) }
describe '.flag!' do
describe '.flag' do
it 'creates a flag when there is none' do
expect { described_class.flag!(user, comment) }.to change{ Flag.count }.by(1)
expect { described_class.flag(user, comment) }.to change{ Flag.count }.by(1)
expect(Flag.last.user).to eq(user)
expect(Flag.last.flaggable).to eq(comment)
end
it 'raises an error if the flag has already been created' do
described_class.flag!(user, comment)
expect { described_class.flag!(user, comment) }.to raise_error(Flag::AlreadyFlaggedError)
it 'does nothing if the flag already exists' do
described_class.flag(user, comment)
expect(described_class.flag(user, comment)).to eq(false)
expect(Flag.by_user_and_flaggable(user, comment).count).to eq(1)
end
it 'increases the flag count' do
expect { described_class.flag!(user, comment) }.to change{ comment.reload.flags_count }.by(1)
expect { described_class.flag(user, comment) }.to change{ comment.reload.flags_count }.by(1)
end
end
describe '.unflag!' do
describe '.unflag' do
it 'raises an error if the flag does not exist' do
expect { described_class.unflag!(user, comment) }.to raise_error(Flag::NotFlaggedError)
expect(described_class.unflag(user, comment)).to eq(false)
end
describe 'when the flag already exists' do
before(:each) { described_class.flag!(user, comment) }
before(:each) { described_class.flag(user, comment) }
it 'removes an existing flag' do
expect { described_class.unflag!(user, comment) }.to change{ Flag.count }.by(-1)
expect { described_class.unflag(user, comment) }.to change{ Flag.count }.by(-1)
end
it 'decreases the flag count' do
expect { described_class.unflag!(user, comment) }.to change{ comment.reload.flags_count }.by(-1)
expect { described_class.unflag(user, comment) }.to change{ comment.reload.flags_count }.by(-1)
end
end
end
describe '.flagged?' do
@@ -48,7 +48,7 @@ describe Flag do
end
it 'returns true when the user has flagged the comment' do
described_class.flag!(user, comment)
described_class.flag(user, comment)
expect(described_class.flagged?(user, comment)).to be
end
end