Commit 7bd66b76 authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'backstage/gb/optimize-some-specs-with-rspec-set' into 'master'

Optimize some tests with RSpec Set

See merge request !14047
parents 1f5ae8cd 6fe19ce9
require 'rails_helper' require 'rails_helper'
RSpec.describe AbuseReport do describe AbuseReport do
subject { create(:abuse_report) } set(:report) { create(:abuse_report) }
let(:user) { create(:admin) } set(:user) { create(:admin) }
subject { report }
it { expect(subject).to be_valid } it { expect(subject).to be_valid }
......
require 'rails_helper' require 'rails_helper'
RSpec.describe Appearance do describe Appearance do
subject { build(:appearance) } subject { build(:appearance) }
it { is_expected.to be_valid } it { is_expected.to be_valid }
......
require 'spec_helper' require 'spec_helper'
describe ChatName do describe ChatName do
subject { create(:chat_name) } set(:chat_name) { create(:chat_name) }
subject { chat_name }
it { is_expected.to belong_to(:service) } it { is_expected.to belong_to(:service) }
it { is_expected.to belong_to(:user) } it { is_expected.to belong_to(:user) }
......
require 'spec_helper' require 'spec_helper'
describe ChatTeam do describe ChatTeam do
subject { create(:chat_team) } set(:chat_team) { create(:chat_team) }
subject { chat_team }
# Associations # Associations
it { is_expected.to belong_to(:namespace) } it { is_expected.to belong_to(:namespace) }
......
require 'spec_helper' require 'spec_helper'
describe Ci::Build do describe Ci::Build do
let(:user) { create(:user) } set(:user) { create(:user) }
let(:project) { create(:project, :repository) } set(:group) { create(:group, :access_requestable) }
let(:build) { create(:ci_build, pipeline: pipeline) } set(:project) { create(:project, :repository, group: group) }
let(:test_trace) { 'This is a test' }
let(:pipeline) do set(:pipeline) do
create(:ci_pipeline, project: project, create(:ci_pipeline, project: project,
sha: project.commit.id, sha: project.commit.id,
ref: project.default_branch, ref: project.default_branch,
status: 'success') status: 'success')
end end
let(:build) { create(:ci_build, pipeline: pipeline) }
it { is_expected.to belong_to(:runner) } it { is_expected.to belong_to(:runner) }
it { is_expected.to belong_to(:trigger_request) } it { is_expected.to belong_to(:trigger_request) }
it { is_expected.to belong_to(:erased_by) } it { is_expected.to belong_to(:erased_by) }
...@@ -282,7 +283,7 @@ describe Ci::Build do ...@@ -282,7 +283,7 @@ describe Ci::Build do
let(:project_regex) { '\(\d+\.\d+\) covered' } let(:project_regex) { '\(\d+\.\d+\) covered' }
before do before do
project.build_coverage_regex = project_regex project.update_column(:build_coverage_regex, project_regex)
end end
context 'and coverage_regex attribute is not set' do context 'and coverage_regex attribute is not set' do
...@@ -1096,9 +1097,6 @@ describe Ci::Build do ...@@ -1096,9 +1097,6 @@ describe Ci::Build do
end end
describe '#repo_url' do describe '#repo_url' do
let(:build) { create(:ci_build) }
let(:project) { build.project }
subject { build.repo_url } subject { build.repo_url }
it { is_expected.to be_a(String) } it { is_expected.to be_a(String) }
...@@ -1199,6 +1197,8 @@ describe Ci::Build do ...@@ -1199,6 +1197,8 @@ describe Ci::Build do
end end
context 'use from gitlab-ci.yml' do context 'use from gitlab-ci.yml' do
let(:pipeline) { create(:ci_pipeline) }
before do before do
stub_ci_pipeline_yaml_file(config) stub_ci_pipeline_yaml_file(config)
end end
...@@ -1442,11 +1442,7 @@ describe Ci::Build do ...@@ -1442,11 +1442,7 @@ describe Ci::Build do
{ key: 'SECRET_KEY', value: 'secret_value', public: false } { key: 'SECRET_KEY', value: 'secret_value', public: false }
end end
let(:group) { create(:group, :access_requestable) }
before do before do
build.project.update(group: group)
create(:ci_group_variable, create(:ci_group_variable,
secret_variable.slice(:key, :value).merge(group: group)) secret_variable.slice(:key, :value).merge(group: group))
end end
...@@ -1459,11 +1455,7 @@ describe Ci::Build do ...@@ -1459,11 +1455,7 @@ describe Ci::Build do
{ key: 'PROTECTED_KEY', value: 'protected_value', public: false } { key: 'PROTECTED_KEY', value: 'protected_value', public: false }
end end
let(:group) { create(:group, :access_requestable) }
before do before do
build.project.update(group: group)
create(:ci_group_variable, create(:ci_group_variable,
:protected, :protected,
protected_variable.slice(:key, :value).merge(group: group)) protected_variable.slice(:key, :value).merge(group: group))
...@@ -1486,6 +1478,10 @@ describe Ci::Build do ...@@ -1486,6 +1478,10 @@ describe Ci::Build do
end end
context 'when the ref is not protected' do context 'when the ref is not protected' do
before do
build.update_column(:ref, 'some/feature')
end
it { is_expected.not_to include(protected_variable) } it { is_expected.not_to include(protected_variable) }
end end
end end
...@@ -1552,6 +1548,8 @@ describe Ci::Build do ...@@ -1552,6 +1548,8 @@ describe Ci::Build do
end end
context 'when yaml_variables are undefined' do context 'when yaml_variables are undefined' do
let(:pipeline) { create(:ci_pipeline, project: project) }
before do before do
build.yaml_variables = nil build.yaml_variables = nil
end end
...@@ -1645,7 +1643,10 @@ describe Ci::Build do ...@@ -1645,7 +1643,10 @@ describe Ci::Build do
before do before do
build.environment = 'production' build.environment = 'production'
allow(project).to receive(:deployment_variables).and_return([deployment_variable])
allow_any_instance_of(Project)
.to receive(:deployment_variables)
.and_return([deployment_variable])
end end
it { is_expected.to include(deployment_variable) } it { is_expected.to include(deployment_variable) }
...@@ -1669,14 +1670,19 @@ describe Ci::Build do ...@@ -1669,14 +1670,19 @@ describe Ci::Build do
before do before do
allow(build).to receive(:predefined_variables) { [build_pre_var] } allow(build).to receive(:predefined_variables) { [build_pre_var] }
allow(project).to receive(:predefined_variables) { [project_pre_var] }
allow(pipeline).to receive(:predefined_variables) { [pipeline_pre_var] }
allow(build).to receive(:yaml_variables) { [build_yaml_var] } allow(build).to receive(:yaml_variables) { [build_yaml_var] }
allow(project).to receive(:secret_variables_for) allow_any_instance_of(Project)
.to receive(:predefined_variables) { [project_pre_var] }
allow_any_instance_of(Project)
.to receive(:secret_variables_for)
.with(ref: 'master', environment: nil) do .with(ref: 'master', environment: nil) do
[create(:ci_variable, key: 'secret', value: 'value')] [create(:ci_variable, key: 'secret', value: 'value')]
end end
allow_any_instance_of(Ci::Pipeline)
.to receive(:predefined_variables) { [pipeline_pre_var] }
end end
it do it do
......
require 'spec_helper' require 'spec_helper'
describe LfsObjectsProject do describe LfsObjectsProject do
subject { create(:lfs_objects_project, project: project) } set(:project) { create(:project) }
let(:project) { create(:project) }
subject do
create(:lfs_objects_project, project: project)
end
describe 'associations' do describe 'associations' do
it { is_expected.to belong_to(:project) } it { is_expected.to belong_to(:project) }
...@@ -11,9 +14,13 @@ describe LfsObjectsProject do ...@@ -11,9 +14,13 @@ describe LfsObjectsProject do
describe 'validation' do describe 'validation' do
it { is_expected.to validate_presence_of(:lfs_object_id) } it { is_expected.to validate_presence_of(:lfs_object_id) }
it { is_expected.to validate_uniqueness_of(:lfs_object_id).scoped_to(:project_id).with_message("already exists in project") }
it { is_expected.to validate_presence_of(:project_id) } it { is_expected.to validate_presence_of(:project_id) }
it 'validates object id' do
is_expected.to validate_uniqueness_of(:lfs_object_id)
.scoped_to(:project_id)
.with_message("already exists in project")
end
end end
describe '#update_project_statistics' do describe '#update_project_statistics' do
......
require "spec_helper" require "spec_helper"
describe API::Services do describe API::Services do
let(:user) { create(:user) } set(:user) { create(:user) }
let(:admin) { create(:admin) } set(:admin) { create(:admin) }
let(:user2) { create(:user) } set(:user2) { create(:user) }
let(:project) { create(:project, creator_id: user.id, namespace: user.namespace) }
set(:project) do
create(:project, creator_id: user.id, namespace: user.namespace)
end
Service.available_services_names.each do |service| Service.available_services_names.each do |service|
describe "PUT /projects/:id/services/#{service.dasherize}" do describe "PUT /projects/:id/services/#{service.dasherize}" do
...@@ -98,8 +101,6 @@ describe API::Services do ...@@ -98,8 +101,6 @@ describe API::Services do
end end
describe 'POST /projects/:id/services/:slug/trigger' do describe 'POST /projects/:id/services/:slug/trigger' do
let!(:project) { create(:project) }
describe 'Mattermost Service' do describe 'Mattermost Service' do
let(:service_name) { 'mattermost_slash_commands' } let(:service_name) { 'mattermost_slash_commands' }
......
require 'spec_helper' require 'spec_helper'
describe Ci::RetryBuildService do describe Ci::RetryBuildService do
let(:user) { create(:user) } set(:user) { create(:user) }
let(:project) { create(:project) } set(:project) { create(:project) }
let(:pipeline) { create(:ci_pipeline, project: project) } set(:pipeline) { create(:ci_pipeline, project: project) }
let(:build) { create(:ci_build, pipeline: pipeline) } let(:build) { create(:ci_build, pipeline: pipeline) }
let(:service) do let(:service) do
...@@ -37,7 +38,7 @@ describe Ci::RetryBuildService do ...@@ -37,7 +38,7 @@ describe Ci::RetryBuildService do
:queued, :coverage, :tags, :allowed_to_fail, :on_tag, :queued, :coverage, :tags, :allowed_to_fail, :on_tag,
:triggered, :trace, :teardown_environment, :triggered, :trace, :teardown_environment,
description: 'my-job', stage: 'test', pipeline: pipeline, description: 'my-job', stage: 'test', pipeline: pipeline,
auto_canceled_by: create(:ci_empty_pipeline)) do |build| auto_canceled_by: create(:ci_empty_pipeline, project: project)) do |build|
## ##
# TODO, workaround for FactoryGirl limitation when having both # TODO, workaround for FactoryGirl limitation when having both
# stage (text) and stage_id (integer) columns in the table. # stage (text) and stage_id (integer) columns in the table.
......
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