Commit 18611f0e authored by Peter Leitzen's avatar Peter Leitzen

Refactor spec for Commits::UpdateService

parent d331377a
...@@ -4,46 +4,75 @@ describe Commits::UpdateService do ...@@ -4,46 +4,75 @@ describe Commits::UpdateService do
let(:project) { create(:project, :repository) } let(:project) { create(:project, :repository) }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:commit) { create(:commit, project: project) } let(:commit) { project.commit }
before do before do
project.add_maintainer(user) project.add_maintainer(user)
end end
describe 'execute' do describe '#execute' do
let(:service) { described_class.new(project, user, opts) } let(:service) { described_class.new(project, user, opts) }
shared_examples 'tagging fails' do
it 'returns nil' do
tagged_commit = service.execute(commit)
expect(tagged_commit).to be_nil
end
it 'does not add a system note' do
service.execute(commit)
description_notes = find_notes('tag')
expect(description_notes).to be_empty
end
end
def find_notes(action)
commit
.notes
.joins(:system_note_metadata)
.where(system_note_metadata: { action: action })
end
context 'valid params' do context 'valid params' do
let(:opts) do let(:opts) do
{ {
tag_name: '1.2.3', tag_name: 'v1.2.3',
tag_message: 'Release' tag_message: 'Release'
} }
end end
it 'tags a commit' do def find_notes(action)
tag_double = double(name: opts[:tag_name]) commit
tag_stub = instance_double(Tags::CreateService) .notes
allow(Tags::CreateService).to receive(:new).and_return(tag_stub) .joins(:system_note_metadata)
allow(tag_stub).to receive(:execute) .where(system_note_metadata: { action: action })
.with(opts[:tag_name], commit.sha, opts[:tag_message], nil) end
.and_return({ status: :success, tag: tag_double })
context 'when tagging succeeds' do
it 'returns the commit' do
tagged_commit = service.execute(commit)
expect(SystemNoteService).to receive(:tag_commit).with(commit, project, user, opts[:tag_name]) expect(tagged_commit).to eq(commit)
end
it 'adds a system note' do
service.execute(commit) service.execute(commit)
description_notes = find_notes('tag')
expect(description_notes.length).to eq(1)
end
end end
it 'fails to tag the commit' do context 'when tagging fails' do
before do
tag_stub = instance_double(Tags::CreateService) tag_stub = instance_double(Tags::CreateService)
allow(Tags::CreateService).to receive(:new).and_return(tag_stub) allow(Tags::CreateService).to receive(:new).and_return(tag_stub)
allow(tag_stub).to receive(:execute) allow(tag_stub).to receive(:execute).and_return({ status: :error })
.with(opts[:tag_name], commit.sha, opts[:tag_message], nil) end
.and_return({ status: :error })
expect(SystemNoteService).not_to receive(:tag_commit)
service.execute(commit) include_examples 'tagging fails'
end end
end end
...@@ -52,12 +81,7 @@ describe Commits::UpdateService do ...@@ -52,12 +81,7 @@ describe Commits::UpdateService do
{} {}
end end
it 'does not call the tag create service' do include_examples 'tagging fails'
expect(Tags::CreateService).not_to receive(:new)
expect(SystemNoteService).not_to receive(:tag_commit)
service.execute(commit)
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