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
subject.recipients = recipients
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
subject.branches_to_be_notified = branches_to_be_notified
end
......@@ -43,7 +45,9 @@ describe EmailsOnPushService do
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
subject.branches_to_be_notified = branches_to_be_notified
end
......@@ -66,71 +70,29 @@ describe EmailsOnPushService do
context 'when emails are enabled on the project' do
before do
create(:protected_branch, project: project, name: 'a-protected-branch')
expect(project).to receive(:emails_disabled?).and_return(true)
end
context 'pushing to the default branch' do
let(:push_data) { { object_kind: 'push', object_attributes: { ref: project.default_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 '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 'sending email', branches_to_be_notified: "default_and_protected"
end
using RSpec::Parameterized::TableSyntax
where(:case_name, :branches_to_be_notified, :branch_being_pushed_to, :expected_action) do
'pushing to a random branch and notification configured for all branches' | 'all' | 'random' | 'sending email'
'pushing to the default branch and notification configured for all branches' | 'all' | 'master' | 'sending email'
'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'
'pushing to the default branch and notification configured for default branch only' | 'default' | 'master' | 'sending email'
'pushing to a protected branch and notification configured for default branch only' | 'default' | 'a-protected-branch' | 'not sending email'
'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'
'pushing to a protected branch and notification configured for protected branches only' | 'protected' | 'a-protected-branch' | 'sending email'
'pushing to a random branch and notification configured for default and protected branches only' | 'default_and_protected' | 'random' | 'not sending email'
'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'
end
context 'pushing to a protected branch' do
before do
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
with_them do
include_examples params[:expected_action], branches_to_be_notified: params[:branches_to_be_notified], branch_being_pushed_to: params[:branch_being_pushed_to]
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