Commit 84c708c3 authored by drew cimino's avatar drew cimino

Restructure spec around method contexts

parent 51d6fab3
......@@ -3,25 +3,16 @@
require 'spec_helper'
RSpec.describe Gitlab::Badge::Coverage::Report do
let(:project) { double('project') }
let_it_be(:successful_pipeline) do
create(:ci_pipeline, :success)
end
let(:project) { build(:project) }
let(:badge) do
described_class.new(project, 'master', opts: { job: job_name }).tap do |new_badge|
allow(new_badge).to receive(:pipeline).and_return(successful_pipeline)
end
described_class.new(project, 'master', opts: { job: job_name })
end
let(:pipeline) { nil }
let(:job_name) { nil }
let(:builds) { [] }
before do
allow(successful_pipeline).to receive(:builds).and_return(Ci::Build.where(id: builds.map(&:id)))
end
describe '#entity' do
it 'describes a coverage' do
expect(badge.entity).to eq 'coverage'
......@@ -40,50 +31,55 @@ RSpec.describe Gitlab::Badge::Coverage::Report do
end
end
shared_examples 'unknown coverage report' do
context 'particular job specified' do
let(:job_name) { '' }
describe '#status' do
before do
allow(badge).to receive(:pipeline).and_return(pipeline)
end
context 'with no pipeline' do
it 'returns nil' do
expect(badge.status).to be_nil
end
end
context 'particular job not specified' do
let(:job_name) { nil }
context 'with no job specified' do
let(:pipeline) { double('pipeline', coverage: 1) }
it 'returns nil' do
expect(badge.status).to be_nil
it 'returns the pipeline coverage value' do
expect(badge.status).to eq(1.00)
end
end
end
context 'when latest successful pipeline exists' do
let(:builds) do
[
create(:ci_build, :success, pipeline: pipeline, name: 'first', coverage: 40),
create(:ci_build, :success, pipeline: pipeline, coverage: 60)
]
end
context 'with an unmatching job name specified' do
let(:job_name) { 'incorrect name' }
let(:pipeline) { create(:ci_pipeline, :success, builds: builds) }
context 'when particular job specified' do
let(:job_name) { 'first' }
let(:builds) do
[
create(:ci_build, :success, name: 'first', coverage: 40),
create(:ci_build, :success, coverage: 60)
]
end
it 'returns coverage for the particular job' do
expect(badge.status).to eq 40
it 'returns nil' do
expect(badge.status).to eq(nil)
end
end
context 'when particular job not specified' do
let(:job_name) { '' }
context 'with a matching job name specified' do
let(:job_name) { 'first' }
let(:pipeline) { create(:ci_pipeline, :success, builds: builds) }
it 'returns arithemetic mean for the pipeline' do
expect(badge.status).to eq 50
let(:builds) do
[
create(:ci_build, :success, name: 'first', coverage: 40),
create(:ci_build, :success, coverage: 60)
]
end
end
end
context 'pipeline does not exist' do
it_behaves_like 'unknown coverage report'
it 'returns nil' do
expect(badge.status).to eq(40.00)
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