Commit b4fc5657 authored by Balasankar "Balu" C's avatar Balasankar "Balu" C

Use table form of rspec tests to be DRY

Signed-off-by: default avatarBalasankar "Balu" C <balasankarc@autistici.org>
parent b0922fbc
...@@ -31,7 +31,9 @@ describe EmailsOnPushService do ...@@ -31,7 +31,9 @@ describe EmailsOnPushService do
subject.recipients = recipients subject.recipients = recipients
end end
shared_examples 'sending email' do |branches_to_be_notified| shared_examples 'sending email' do |branches_to_be_notified, branch_being_pushed_to|
let(:push_data) { { object_kind: 'push', object_attributes: { ref: branch_being_pushed_to } } }
before do before do
subject.branches_to_be_notified = branches_to_be_notified subject.branches_to_be_notified = branches_to_be_notified
end end
...@@ -43,7 +45,9 @@ describe EmailsOnPushService do ...@@ -43,7 +45,9 @@ describe EmailsOnPushService do
end end
end end
shared_examples 'not sending email' do |branches_to_be_notified| shared_examples 'not sending email' do |branches_to_be_notified, branch_being_pushed_to|
let(:push_data) { { object_kind: 'push', object_attributes: { ref: branch_being_pushed_to } } }
before do before do
subject.branches_to_be_notified = branches_to_be_notified subject.branches_to_be_notified = branches_to_be_notified
end end
...@@ -66,71 +70,29 @@ describe EmailsOnPushService do ...@@ -66,71 +70,29 @@ describe EmailsOnPushService do
context 'when emails are enabled on the project' do context 'when emails are enabled on the project' do
before do before do
create(:protected_branch, project: project, name: 'a-protected-branch')
expect(project).to receive(:emails_disabled?).and_return(true) expect(project).to receive(:emails_disabled?).and_return(true)
end end
context 'pushing to the default branch' do using RSpec::Parameterized::TableSyntax
let(:push_data) { { object_kind: 'push', object_attributes: { ref: project.default_branch } } }
where(:case_name, :branches_to_be_notified, :branch_being_pushed_to, :expected_action) do
context 'when configured to send email on pushes to any branch' do 'pushing to a random branch and notification configured for all branches' | 'all' | 'random' | 'sending email'
it_behaves_like 'sending email', branches_to_be_notified: "all" 'pushing to the default branch and notification configured for all branches' | 'all' | 'master' | 'sending email'
end 'pushing to a protected branch and notification configured for all branches' | 'all' | 'a-protected-branch' | 'sending email'
'pushing to a random branch and notification configured for default branch only' | 'default' | 'random' | 'not sending email'
context 'when configured to send email on pushes to default branch' do 'pushing to the default branch and notification configured for default branch only' | 'default' | 'master' | 'sending email'
it_behaves_like 'sending email', branches_to_be_notified: "default" 'pushing to a protected branch and notification configured for default branch only' | 'default' | 'a-protected-branch' | 'not sending email'
end 'pushing to a random branch and notification configured for protected branches only' | 'protected' | 'random' | 'not sending email'
'pushing to the default branch and notification configured for protected branches only' | 'protected' | 'master' | 'not sending email'
context 'when configured to send email on pushes to protected branches only' do 'pushing to a protected branch and notification configured for protected branches only' | 'protected' | 'a-protected-branch' | 'sending email'
it_behaves_like 'not sending email', branches_to_be_notified: "protected" 'pushing to a random branch and notification configured for default and protected branches only' | 'default_and_protected' | 'random' | 'not sending email'
end 'pushing to the default branch and notification configured for default and protected branches only' | 'default_and_protected' | 'master' | 'sending email'
'pushing to a protected branch and notification configured for default and protected branches only' | 'default_and_protected' | 'a-protected-branch' | 'sending email'
context 'when configured to send email on pushes to default and protected branches only' do
it_behaves_like 'sending email', branches_to_be_notified: "default_and_protected"
end
end end
context 'pushing to a protected branch' do with_them do
before do include_examples params[:expected_action], branches_to_be_notified: params[:branches_to_be_notified], branch_being_pushed_to: params[:branch_being_pushed_to]
create(:protected_branch, project: project, name: 'a-protected-branch')
end
let(:push_data) { { object_kind: 'push', object_attributes: { ref: 'a-protected-branch' } } }
context 'when configured to send email on pushes to any branch' do
it_behaves_like 'sending email', branches_to_be_notified: "all"
end
context 'when configured to send email on pushes to default branch' do
it_behaves_like 'not sending email', branches_to_be_notified: "default"
end
context 'when configured to send email on pushes to protected branches only' do
it_behaves_like 'sending email', branches_to_be_notified: "protected"
end
context 'when configured to send email on pushes to default and protected branches only' do
it_behaves_like 'sending email', branches_to_be_notified: "default_and_protected"
end
end
context 'pushing to a random branch' do
let(:push_data) { { object_kind: 'push', object_attributes: { ref: 'a-random-branch' } } }
context 'when configured to send email on pushes to any branch' do
it_behaves_like 'sending email', branches_to_be_notified: "all"
end
context 'when configured to send email on pushes to default branch' do
it_behaves_like 'not sending email', branches_to_be_notified: "default"
end
context 'when configured to send email on pushes to protected branches only' do
it_behaves_like 'not sending email', branches_to_be_notified: "protected"
end
context 'when configured to send email on pushes to default and protected branches only' do
it_behaves_like 'not sending email', branches_to_be_notified: "default_and_protected"
end
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment