Commit 182d3fb8 authored by Robert Speicher's avatar Robert Speicher

Merge branch 'let_it_be' into 'master'

let_it_be for spec/controllers

See merge request gitlab-org/gitlab!17680
parents 40e31aa0 b0409589
...@@ -202,8 +202,36 @@ so we need to set some guidelines for their use going forward: ...@@ -202,8 +202,36 @@ so we need to set some guidelines for their use going forward:
order is required, otherwise `let` will suffice. Remember that `let` is lazy and won't order is required, otherwise `let` will suffice. Remember that `let` is lazy and won't
be evaluated until it is referenced. be evaluated until it is referenced.
### `let_it_be` variables
In some cases there is no need to recreate the same object for tests
again for each example. For example, a project is needed to test issues
on the same project, one project will do for the entire file. This can
be achieved by using
[`let_it_be`](https://test-prof.evilmartians.io/#/let_it_be) variables
from the [`test-prof` gem](https://rubygems.org/gems/test-prof).
Note that if you modify an object defined inside a `let_it_be` block,
then you will need to reload the object as needed, or specify the `reload`
option to reload for every example.
```
let_it_be(:project, reload: true) { create(:project) }
```
You can also specify the `refind` option as well to completely load a
new object.
```
let_it_be(:project, refind: true) { create(:project) }
```
### `set` variables ### `set` variables
NOTE: **Note:**
We are incrementally removing `set` in favour of `let_it_be`. See the
[removal issue](https://gitlab.com/gitlab-org/gitlab/issues/27922).
In some cases there is no need to recreate the same object for tests again for In some cases there is no need to recreate the same object for tests again for
each example. For example, a project is needed to test issues on the same each example. For example, a project is needed to test issues on the same
project, one project will do for the entire file. This can be achieved by using project, one project will do for the entire file. This can be achieved by using
......
...@@ -469,7 +469,7 @@ describe Admin::ClustersController do ...@@ -469,7 +469,7 @@ describe Admin::ClustersController do
end end
describe 'security' do describe 'security' do
set(:cluster) { create(:cluster, :provided_by_gcp, :instance) } let_it_be(:cluster) { create(:cluster, :provided_by_gcp, :instance) }
it { expect { put_update }.to be_allowed_for(:admin) } it { expect { put_update }.to be_allowed_for(:admin) }
it { expect { put_update }.to be_denied_for(:user) } it { expect { put_update }.to be_denied_for(:user) }
...@@ -531,7 +531,7 @@ describe Admin::ClustersController do ...@@ -531,7 +531,7 @@ describe Admin::ClustersController do
end end
describe 'security' do describe 'security' do
set(:cluster) { create(:cluster, :provided_by_gcp, :production_environment, :instance) } let_it_be(:cluster) { create(:cluster, :provided_by_gcp, :production_environment, :instance) }
it { expect { delete_destroy }.to be_allowed_for(:admin) } it { expect { delete_destroy }.to be_allowed_for(:admin) }
it { expect { delete_destroy }.to be_denied_for(:user) } it { expect { delete_destroy }.to be_denied_for(:user) }
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
require 'spec_helper' require 'spec_helper'
describe Admin::RequestsProfilesController do describe Admin::RequestsProfilesController do
set(:admin) { create(:admin) } let_it_be(:admin) { create(:admin) }
before do before do
sign_in(admin) sign_in(admin)
......
...@@ -4,7 +4,7 @@ require 'spec_helper' ...@@ -4,7 +4,7 @@ require 'spec_helper'
describe Admin::UsersController do describe Admin::UsersController do
let(:user) { create(:user) } let(:user) { create(:user) }
set(:admin) { create(:admin) } let_it_be(:admin) { create(:admin) }
before do before do
sign_in(admin) sign_in(admin)
......
...@@ -6,7 +6,7 @@ describe Groups::ClustersController do ...@@ -6,7 +6,7 @@ describe Groups::ClustersController do
include AccessMatchersForController include AccessMatchersForController
include GoogleApi::CloudPlatformHelpers include GoogleApi::CloudPlatformHelpers
set(:group) { create(:group) } let_it_be(:group) { create(:group) }
let(:user) { create(:user) } let(:user) { create(:user) }
...@@ -536,7 +536,7 @@ describe Groups::ClustersController do ...@@ -536,7 +536,7 @@ describe Groups::ClustersController do
end end
describe 'security' do describe 'security' do
set(:cluster) { create(:cluster, :provided_by_gcp, cluster_type: :group_type, groups: [group]) } let_it_be(:cluster) { create(:cluster, :provided_by_gcp, cluster_type: :group_type, groups: [group]) }
it { expect { go }.to be_allowed_for(:admin) } it { expect { go }.to be_allowed_for(:admin) }
it { expect { go }.to be_allowed_for(:owner).of(group) } it { expect { go }.to be_allowed_for(:owner).of(group) }
...@@ -604,7 +604,7 @@ describe Groups::ClustersController do ...@@ -604,7 +604,7 @@ describe Groups::ClustersController do
end end
describe 'security' do describe 'security' do
set(:cluster) { create(:cluster, :provided_by_gcp, :production_environment, cluster_type: :group_type, groups: [group]) } let_it_be(:cluster) { create(:cluster, :provided_by_gcp, :production_environment, cluster_type: :group_type, groups: [group]) }
it { expect { go }.to be_allowed_for(:admin) } it { expect { go }.to be_allowed_for(:admin) }
it { expect { go }.to be_allowed_for(:owner).of(group) } it { expect { go }.to be_allowed_for(:owner).of(group) }
......
...@@ -3,9 +3,9 @@ ...@@ -3,9 +3,9 @@
require 'spec_helper' require 'spec_helper'
describe Groups::LabelsController do describe Groups::LabelsController do
set(:group) { create(:group) } let_it_be(:group) { create(:group) }
set(:user) { create(:user) } let_it_be(:user) { create(:user) }
set(:project) { create(:project, namespace: group) } let_it_be(:project) { create(:project, namespace: group) }
before do before do
group.add_owner(user) group.add_owner(user)
...@@ -14,8 +14,8 @@ describe Groups::LabelsController do ...@@ -14,8 +14,8 @@ describe Groups::LabelsController do
end end
describe 'GET #index' do describe 'GET #index' do
set(:label_1) { create(:label, project: project, title: 'label_1') } let_it_be(:label_1) { create(:label, project: project, title: 'label_1') }
set(:group_label_1) { create(:group_label, group: group, title: 'group_label_1') } let_it_be(:group_label_1) { create(:group_label, group: group, title: 'group_label_1') }
it 'returns group and project labels by default' do it 'returns group and project labels by default' do
get :index, params: { group_id: group }, format: :json get :index, params: { group_id: group }, format: :json
...@@ -25,8 +25,8 @@ describe Groups::LabelsController do ...@@ -25,8 +25,8 @@ describe Groups::LabelsController do
end end
context 'with ancestor group' do context 'with ancestor group' do
set(:subgroup) { create(:group, parent: group) } let_it_be(:subgroup) { create(:group, parent: group) }
set(:subgroup_label_1) { create(:group_label, group: subgroup, title: 'subgroup_label_1') } let_it_be(:subgroup_label_1) { create(:group_label, group: subgroup, title: 'subgroup_label_1') }
before do before do
subgroup.add_owner(user) subgroup.add_owner(user)
......
...@@ -17,9 +17,9 @@ describe Groups::SharedProjectsController do ...@@ -17,9 +17,9 @@ describe Groups::SharedProjectsController do
).execute(group) ).execute(group)
end end
set(:group) { create(:group) } let_it_be(:group) { create(:group) }
set(:user) { create(:user) } let_it_be(:user) { create(:user) }
set(:shared_project) do let_it_be(:shared_project) do
shared_project = create(:project, namespace: user.namespace) shared_project = create(:project, namespace: user.namespace)
share_project(shared_project) share_project(shared_project)
......
...@@ -39,7 +39,7 @@ describe Import::BitbucketServerController do ...@@ -39,7 +39,7 @@ describe Import::BitbucketServerController do
assign_session_tokens assign_session_tokens
end end
set(:project) { create(:project) } let_it_be(:project) { create(:project) }
it 'returns the new project' do it 'returns the new project' do
allow(Gitlab::BitbucketServerImport::ProjectCreator) allow(Gitlab::BitbucketServerImport::ProjectCreator)
......
...@@ -3,8 +3,8 @@ ...@@ -3,8 +3,8 @@
require 'spec_helper' require 'spec_helper'
describe Import::GitlabProjectsController do describe Import::GitlabProjectsController do
set(:namespace) { create(:namespace) } let_it_be(:namespace) { create(:namespace) }
set(:user) { namespace.owner } let_it_be(:user) { namespace.owner }
let(:file) { fixture_file_upload('spec/fixtures/project_export.tar.gz', 'text/plain') } let(:file) { fixture_file_upload('spec/fixtures/project_export.tar.gz', 'text/plain') }
before do before do
......
...@@ -4,9 +4,9 @@ require 'spec_helper' ...@@ -4,9 +4,9 @@ require 'spec_helper'
describe Projects::ArtifactsController do describe Projects::ArtifactsController do
let(:user) { project.owner } let(:user) { project.owner }
set(:project) { create(:project, :repository, :public) } let_it_be(:project) { create(:project, :repository, :public) }
set(:pipeline) do let_it_be(:pipeline, reload: true) do
create(:ci_pipeline, create(:ci_pipeline,
project: project, project: project,
sha: project.commit.sha, sha: project.commit.sha,
...@@ -48,7 +48,7 @@ describe Projects::ArtifactsController do ...@@ -48,7 +48,7 @@ describe Projects::ArtifactsController do
it 'paginates artifacts' do it 'paginates artifacts' do
subject subject
expect(assigns(:artifacts)).to contain_exactly(project.job_artifacts.last) expect(assigns(:artifacts)).to contain_exactly(project.reload.job_artifacts.last)
end end
end end
end end
......
...@@ -3,10 +3,10 @@ ...@@ -3,10 +3,10 @@
require 'spec_helper' require 'spec_helper'
describe Projects::AutocompleteSourcesController do describe Projects::AutocompleteSourcesController do
set(:group) { create(:group) } let_it_be(:group) { create(:group) }
set(:project) { create(:project, namespace: group) } let_it_be(:project) { create(:project, namespace: group) }
set(:issue) { create(:issue, project: project) } let_it_be(:issue) { create(:issue, project: project) }
set(:user) { create(:user) } let_it_be(:user) { create(:user) }
describe 'GET members' do describe 'GET members' do
before do before do
......
...@@ -7,7 +7,7 @@ describe Projects::ClustersController do ...@@ -7,7 +7,7 @@ describe Projects::ClustersController do
include GoogleApi::CloudPlatformHelpers include GoogleApi::CloudPlatformHelpers
include KubernetesHelpers include KubernetesHelpers
set(:project) { create(:project) } let_it_be(:project) { create(:project) }
let(:user) { create(:user) } let(:user) { create(:user) }
...@@ -536,7 +536,7 @@ describe Projects::ClustersController do ...@@ -536,7 +536,7 @@ describe Projects::ClustersController do
end end
describe 'security' do describe 'security' do
set(:cluster) { create(:cluster, :provided_by_gcp, projects: [project]) } let_it_be(:cluster) { create(:cluster, :provided_by_gcp, projects: [project]) }
it { expect { go }.to be_allowed_for(:admin) } it { expect { go }.to be_allowed_for(:admin) }
it { expect { go }.to be_allowed_for(:owner).of(project) } it { expect { go }.to be_allowed_for(:owner).of(project) }
...@@ -605,7 +605,7 @@ describe Projects::ClustersController do ...@@ -605,7 +605,7 @@ describe Projects::ClustersController do
end end
describe 'security' do describe 'security' do
set(:cluster) { create(:cluster, :provided_by_gcp, :production_environment, projects: [project]) } let_it_be(:cluster) { create(:cluster, :provided_by_gcp, :production_environment, projects: [project]) }
it { expect { go }.to be_allowed_for(:admin) } it { expect { go }.to be_allowed_for(:admin) }
it { expect { go }.to be_allowed_for(:owner).of(project) } it { expect { go }.to be_allowed_for(:owner).of(project) }
......
...@@ -3,8 +3,9 @@ ...@@ -3,8 +3,9 @@
require 'spec_helper' require 'spec_helper'
describe Projects::CommitController do describe Projects::CommitController do
set(:project) { create(:project, :repository) } let_it_be(:project) { create(:project, :repository) }
set(:user) { create(:user) } let_it_be(:user) { create(:user) }
let(:commit) { project.commit("master") } let(:commit) { project.commit("master") }
let(:master_pickable_sha) { '7d3b0f7cff5f37573aea97cebfd5692ea1689924' } let(:master_pickable_sha) { '7d3b0f7cff5f37573aea97cebfd5692ea1689924' }
let(:master_pickable_commit) { project.commit(master_pickable_sha) } let(:master_pickable_commit) { project.commit(master_pickable_sha) }
......
...@@ -3,9 +3,9 @@ ...@@ -3,9 +3,9 @@
require 'spec_helper' require 'spec_helper'
describe Projects::Environments::PrometheusApiController do describe Projects::Environments::PrometheusApiController do
set(:project) { create(:project) } let_it_be(:project) { create(:project) }
set(:environment) { create(:environment, project: project) } let_it_be(:environment) { create(:environment, project: project) }
set(:user) { create(:user) } let_it_be(:user) { create(:user) }
before do before do
project.add_reporter(user) project.add_reporter(user)
......
...@@ -5,10 +5,10 @@ require 'spec_helper' ...@@ -5,10 +5,10 @@ require 'spec_helper'
describe Projects::EnvironmentsController do describe Projects::EnvironmentsController do
include MetricsDashboardHelpers include MetricsDashboardHelpers
set(:user) { create(:user) } let_it_be(:user) { create(:user) }
set(:project) { create(:project) } let_it_be(:project) { create(:project) }
set(:environment) do let_it_be(:environment) do
create(:environment, name: 'production', project: project) create(:environment, name: 'production', project: project)
end end
......
...@@ -3,8 +3,8 @@ ...@@ -3,8 +3,8 @@
require 'spec_helper' require 'spec_helper'
describe Projects::ErrorTrackingController do describe Projects::ErrorTrackingController do
set(:project) { create(:project) } let_it_be(:project) { create(:project) }
set(:user) { create(:user) } let_it_be(:user) { create(:user) }
before do before do
sign_in(user) sign_in(user)
......
...@@ -6,7 +6,7 @@ describe Projects::MirrorsController do ...@@ -6,7 +6,7 @@ describe Projects::MirrorsController do
include ReactiveCachingHelpers include ReactiveCachingHelpers
describe 'setting up a remote mirror' do describe 'setting up a remote mirror' do
set(:project) { create(:project, :repository) } let_it_be(:project) { create(:project, :repository) }
context 'when the current project is not a mirror' do context 'when the current project is not a mirror' do
it 'allows to create a remote mirror' do it 'allows to create a remote mirror' do
......
...@@ -5,9 +5,9 @@ require 'spec_helper' ...@@ -5,9 +5,9 @@ require 'spec_helper'
describe Projects::PipelineSchedulesController do describe Projects::PipelineSchedulesController do
include AccessMatchersForController include AccessMatchersForController
set(:user) { create(:user) } let_it_be(:user) { create(:user) }
set(:project) { create(:project, :public, :repository) } let_it_be(:project) { create(:project, :public, :repository) }
set(:pipeline_schedule) { create(:ci_pipeline_schedule, project: project) } let_it_be(:pipeline_schedule) { create(:ci_pipeline_schedule, project: project) }
before do before do
project.add_developer(user) project.add_developer(user)
...@@ -60,8 +60,6 @@ describe Projects::PipelineSchedulesController do ...@@ -60,8 +60,6 @@ describe Projects::PipelineSchedulesController do
end end
describe 'GET #new' do describe 'GET #new' do
set(:user) { create(:user) }
before do before do
project.add_developer(user) project.add_developer(user)
sign_in(user) sign_in(user)
...@@ -77,8 +75,6 @@ describe Projects::PipelineSchedulesController do ...@@ -77,8 +75,6 @@ describe Projects::PipelineSchedulesController do
describe 'POST #create' do describe 'POST #create' do
describe 'functionality' do describe 'functionality' do
set(:user) { create(:user) }
before do before do
project.add_developer(user) project.add_developer(user)
sign_in(user) sign_in(user)
...@@ -149,7 +145,6 @@ describe Projects::PipelineSchedulesController do ...@@ -149,7 +145,6 @@ describe Projects::PipelineSchedulesController do
describe 'PUT #update' do describe 'PUT #update' do
describe 'functionality' do describe 'functionality' do
set(:user) { create(:user) }
let!(:pipeline_schedule) { create(:ci_pipeline_schedule, project: project, owner: user) } let!(:pipeline_schedule) { create(:ci_pipeline_schedule, project: project, owner: user) }
before do before do
...@@ -383,7 +378,6 @@ describe Projects::PipelineSchedulesController do ...@@ -383,7 +378,6 @@ describe Projects::PipelineSchedulesController do
end end
describe 'POST #play', :clean_gitlab_redis_cache do describe 'POST #play', :clean_gitlab_redis_cache do
set(:user) { create(:user) }
let(:ref) { 'master' } let(:ref) { 'master' }
before do before do
...@@ -442,8 +436,6 @@ describe Projects::PipelineSchedulesController do ...@@ -442,8 +436,6 @@ describe Projects::PipelineSchedulesController do
end end
describe 'DELETE #destroy' do describe 'DELETE #destroy' do
set(:user) { create(:user) }
context 'when a developer makes the request' do context 'when a developer makes the request' do
before do before do
project.add_developer(user) project.add_developer(user)
......
...@@ -5,7 +5,7 @@ require 'spec_helper' ...@@ -5,7 +5,7 @@ require 'spec_helper'
describe Projects::PipelinesController do describe Projects::PipelinesController do
include ApiHelpers include ApiHelpers
set(:user) { create(:user) } let_it_be(:user) { create(:user) }
let(:project) { create(:project, :public, :repository) } let(:project) { create(:project, :public, :repository) }
let(:feature) { ProjectFeature::ENABLED } let(:feature) { ProjectFeature::ENABLED }
......
...@@ -3,8 +3,8 @@ ...@@ -3,8 +3,8 @@
require 'spec_helper' require 'spec_helper'
describe Projects::PipelinesSettingsController do describe Projects::PipelinesSettingsController do
set(:user) { create(:user) } let_it_be(:user) { create(:user) }
set(:project_auto_devops) { create(:project_auto_devops) } let_it_be(:project_auto_devops) { create(:project_auto_devops) }
let(:project) { project_auto_devops.project } let(:project) { project_auto_devops.project }
before do before do
......
...@@ -3,8 +3,8 @@ ...@@ -3,8 +3,8 @@
require('spec_helper') require('spec_helper')
describe Projects::Settings::CiCdController do describe Projects::Settings::CiCdController do
set(:user) { create(:user) } let_it_be(:user) { create(:user) }
set(:project_auto_devops) { create(:project_auto_devops) } let_it_be(:project_auto_devops) { create(:project_auto_devops) }
let(:project) { project_auto_devops.project } let(:project) { project_auto_devops.project }
before do before do
......
...@@ -3,8 +3,8 @@ ...@@ -3,8 +3,8 @@
require 'spec_helper' require 'spec_helper'
describe Projects::Settings::OperationsController do describe Projects::Settings::OperationsController do
set(:user) { create(:user) } let_it_be(:user) { create(:user) }
set(:project) { create(:project) } let_it_be(:project) { create(:project) }
before do before do
sign_in(user) sign_in(user)
......
...@@ -3,8 +3,8 @@ ...@@ -3,8 +3,8 @@
require 'spec_helper' require 'spec_helper'
describe Projects::WikisController do describe Projects::WikisController do
set(:project) { create(:project, :public, :repository) } let_it_be(:project) { create(:project, :public, :repository) }
set(:user) { project.owner } let(:user) { project.owner }
let(:project_wiki) { ProjectWiki.new(project, user) } let(:project_wiki) { ProjectWiki.new(project, user) }
let(:wiki) { project_wiki.wiki } let(:wiki) { project_wiki.wiki }
let(:wiki_title) { 'page title test' } let(:wiki_title) { 'page title test' }
......
...@@ -149,7 +149,7 @@ describe ProjectsController do ...@@ -149,7 +149,7 @@ describe ProjectsController do
end end
context 'when the storage is not available', :broken_storage do context 'when the storage is not available', :broken_storage do
set(:project) { create(:project, :broken_storage) } let_it_be(:project) { create(:project, :broken_storage) }
before do before do
project.add_developer(user) project.add_developer(user)
......
...@@ -67,7 +67,7 @@ describe SearchController do ...@@ -67,7 +67,7 @@ describe SearchController do
using RSpec::Parameterized::TableSyntax using RSpec::Parameterized::TableSyntax
render_views render_views
set(:project) { create(:project, :public, :repository, :wiki_repo) } let_it_be(:project) { create(:project, :public, :repository, :wiki_repo) }
before do before do
expect(::Gitlab::GitalyClient).to receive(:allow_ref_name_caching).and_call_original expect(::Gitlab::GitalyClient).to receive(:allow_ref_name_caching).and_call_original
......
...@@ -10,6 +10,7 @@ require 'rspec/rails' ...@@ -10,6 +10,7 @@ require 'rspec/rails'
require 'shoulda/matchers' require 'shoulda/matchers'
require 'rspec/retry' require 'rspec/retry'
require 'rspec-parameterized' require 'rspec-parameterized'
require "test_prof/recipes/rspec/let_it_be"
rspec_profiling_is_configured = rspec_profiling_is_configured =
ENV['RSPEC_PROFILING_POSTGRES_URL'].present? || ENV['RSPEC_PROFILING_POSTGRES_URL'].present? ||
......
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