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:
order is required, otherwise `let` will suffice. Remember that `let` is lazy and won't
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
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
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
......
......@@ -469,7 +469,7 @@ describe Admin::ClustersController do
end
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_denied_for(:user) }
......@@ -531,7 +531,7 @@ describe Admin::ClustersController do
end
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_denied_for(:user) }
......
......@@ -3,7 +3,7 @@
require 'spec_helper'
describe Admin::RequestsProfilesController do
set(:admin) { create(:admin) }
let_it_be(:admin) { create(:admin) }
before do
sign_in(admin)
......
......@@ -4,7 +4,7 @@ require 'spec_helper'
describe Admin::UsersController do
let(:user) { create(:user) }
set(:admin) { create(:admin) }
let_it_be(:admin) { create(:admin) }
before do
sign_in(admin)
......
......@@ -6,7 +6,7 @@ describe Groups::ClustersController do
include AccessMatchersForController
include GoogleApi::CloudPlatformHelpers
set(:group) { create(:group) }
let_it_be(:group) { create(:group) }
let(:user) { create(:user) }
......@@ -536,7 +536,7 @@ describe Groups::ClustersController do
end
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(:owner).of(group) }
......@@ -604,7 +604,7 @@ describe Groups::ClustersController do
end
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(:owner).of(group) }
......
......@@ -3,9 +3,9 @@
require 'spec_helper'
describe Groups::LabelsController do
set(:group) { create(:group) }
set(:user) { create(:user) }
set(:project) { create(:project, namespace: group) }
let_it_be(:group) { create(:group) }
let_it_be(:user) { create(:user) }
let_it_be(:project) { create(:project, namespace: group) }
before do
group.add_owner(user)
......@@ -14,8 +14,8 @@ describe Groups::LabelsController do
end
describe 'GET #index' do
set(: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(:label_1) { create(:label, project: project, title: '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
get :index, params: { group_id: group }, format: :json
......@@ -25,8 +25,8 @@ describe Groups::LabelsController do
end
context 'with ancestor group' do
set(:subgroup) { create(:group, parent: group) }
set(:subgroup_label_1) { create(:group_label, group: subgroup, title: 'subgroup_label_1') }
let_it_be(:subgroup) { create(:group, parent: group) }
let_it_be(:subgroup_label_1) { create(:group_label, group: subgroup, title: 'subgroup_label_1') }
before do
subgroup.add_owner(user)
......
......@@ -17,9 +17,9 @@ describe Groups::SharedProjectsController do
).execute(group)
end
set(:group) { create(:group) }
set(:user) { create(:user) }
set(:shared_project) do
let_it_be(:group) { create(:group) }
let_it_be(:user) { create(:user) }
let_it_be(:shared_project) do
shared_project = create(:project, namespace: user.namespace)
share_project(shared_project)
......
......@@ -39,7 +39,7 @@ describe Import::BitbucketServerController do
assign_session_tokens
end
set(:project) { create(:project) }
let_it_be(:project) { create(:project) }
it 'returns the new project' do
allow(Gitlab::BitbucketServerImport::ProjectCreator)
......
......@@ -3,8 +3,8 @@
require 'spec_helper'
describe Import::GitlabProjectsController do
set(:namespace) { create(:namespace) }
set(:user) { namespace.owner }
let_it_be(:namespace) { create(:namespace) }
let_it_be(:user) { namespace.owner }
let(:file) { fixture_file_upload('spec/fixtures/project_export.tar.gz', 'text/plain') }
before do
......
......@@ -4,9 +4,9 @@ require 'spec_helper'
describe Projects::ArtifactsController do
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,
project: project,
sha: project.commit.sha,
......@@ -48,7 +48,7 @@ describe Projects::ArtifactsController do
it 'paginates artifacts' do
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
......
......@@ -3,10 +3,10 @@
require 'spec_helper'
describe Projects::AutocompleteSourcesController do
set(:group) { create(:group) }
set(:project) { create(:project, namespace: group) }
set(:issue) { create(:issue, project: project) }
set(:user) { create(:user) }
let_it_be(:group) { create(:group) }
let_it_be(:project) { create(:project, namespace: group) }
let_it_be(:issue) { create(:issue, project: project) }
let_it_be(:user) { create(:user) }
describe 'GET members' do
before do
......
......@@ -7,7 +7,7 @@ describe Projects::ClustersController do
include GoogleApi::CloudPlatformHelpers
include KubernetesHelpers
set(:project) { create(:project) }
let_it_be(:project) { create(:project) }
let(:user) { create(:user) }
......@@ -536,7 +536,7 @@ describe Projects::ClustersController do
end
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(:owner).of(project) }
......@@ -605,7 +605,7 @@ describe Projects::ClustersController do
end
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(:owner).of(project) }
......
......@@ -3,9 +3,10 @@
require 'spec_helper'
describe Projects::CommitController do
set(:project) { create(:project, :repository) }
set(:user) { create(:user) }
let(:commit) { project.commit("master") }
let_it_be(:project) { create(:project, :repository) }
let_it_be(:user) { create(:user) }
let(:commit) { project.commit("master") }
let(:master_pickable_sha) { '7d3b0f7cff5f37573aea97cebfd5692ea1689924' }
let(:master_pickable_commit) { project.commit(master_pickable_sha) }
......
......@@ -3,9 +3,9 @@
require 'spec_helper'
describe Projects::Environments::PrometheusApiController do
set(:project) { create(:project) }
set(:environment) { create(:environment, project: project) }
set(:user) { create(:user) }
let_it_be(:project) { create(:project) }
let_it_be(:environment) { create(:environment, project: project) }
let_it_be(:user) { create(:user) }
before do
project.add_reporter(user)
......
......@@ -5,10 +5,10 @@ require 'spec_helper'
describe Projects::EnvironmentsController do
include MetricsDashboardHelpers
set(:user) { create(:user) }
set(:project) { create(:project) }
let_it_be(:user) { create(:user) }
let_it_be(:project) { create(:project) }
set(:environment) do
let_it_be(:environment) do
create(:environment, name: 'production', project: project)
end
......
......@@ -3,8 +3,8 @@
require 'spec_helper'
describe Projects::ErrorTrackingController do
set(:project) { create(:project) }
set(:user) { create(:user) }
let_it_be(:project) { create(:project) }
let_it_be(:user) { create(:user) }
before do
sign_in(user)
......
......@@ -6,7 +6,7 @@ describe Projects::MirrorsController do
include ReactiveCachingHelpers
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
it 'allows to create a remote mirror' do
......
......@@ -5,9 +5,9 @@ require 'spec_helper'
describe Projects::PipelineSchedulesController do
include AccessMatchersForController
set(:user) { create(:user) }
set(:project) { create(:project, :public, :repository) }
set(:pipeline_schedule) { create(:ci_pipeline_schedule, project: project) }
let_it_be(:user) { create(:user) }
let_it_be(:project) { create(:project, :public, :repository) }
let_it_be(:pipeline_schedule) { create(:ci_pipeline_schedule, project: project) }
before do
project.add_developer(user)
......@@ -60,8 +60,6 @@ describe Projects::PipelineSchedulesController do
end
describe 'GET #new' do
set(:user) { create(:user) }
before do
project.add_developer(user)
sign_in(user)
......@@ -77,8 +75,6 @@ describe Projects::PipelineSchedulesController do
describe 'POST #create' do
describe 'functionality' do
set(:user) { create(:user) }
before do
project.add_developer(user)
sign_in(user)
......@@ -149,7 +145,6 @@ describe Projects::PipelineSchedulesController do
describe 'PUT #update' do
describe 'functionality' do
set(:user) { create(:user) }
let!(:pipeline_schedule) { create(:ci_pipeline_schedule, project: project, owner: user) }
before do
......@@ -383,7 +378,6 @@ describe Projects::PipelineSchedulesController do
end
describe 'POST #play', :clean_gitlab_redis_cache do
set(:user) { create(:user) }
let(:ref) { 'master' }
before do
......@@ -442,8 +436,6 @@ describe Projects::PipelineSchedulesController do
end
describe 'DELETE #destroy' do
set(:user) { create(:user) }
context 'when a developer makes the request' do
before do
project.add_developer(user)
......
......@@ -5,7 +5,7 @@ require 'spec_helper'
describe Projects::PipelinesController do
include ApiHelpers
set(:user) { create(:user) }
let_it_be(:user) { create(:user) }
let(:project) { create(:project, :public, :repository) }
let(:feature) { ProjectFeature::ENABLED }
......
......@@ -3,8 +3,8 @@
require 'spec_helper'
describe Projects::PipelinesSettingsController do
set(:user) { create(:user) }
set(:project_auto_devops) { create(:project_auto_devops) }
let_it_be(:user) { create(:user) }
let_it_be(:project_auto_devops) { create(:project_auto_devops) }
let(:project) { project_auto_devops.project }
before do
......
......@@ -3,8 +3,8 @@
require('spec_helper')
describe Projects::Settings::CiCdController do
set(:user) { create(:user) }
set(:project_auto_devops) { create(:project_auto_devops) }
let_it_be(:user) { create(:user) }
let_it_be(:project_auto_devops) { create(:project_auto_devops) }
let(:project) { project_auto_devops.project }
before do
......
......@@ -3,8 +3,8 @@
require 'spec_helper'
describe Projects::Settings::OperationsController do
set(:user) { create(:user) }
set(:project) { create(:project) }
let_it_be(:user) { create(:user) }
let_it_be(:project) { create(:project) }
before do
sign_in(user)
......
......@@ -3,8 +3,8 @@
require 'spec_helper'
describe Projects::WikisController do
set(:project) { create(:project, :public, :repository) }
set(:user) { project.owner }
let_it_be(:project) { create(:project, :public, :repository) }
let(:user) { project.owner }
let(:project_wiki) { ProjectWiki.new(project, user) }
let(:wiki) { project_wiki.wiki }
let(:wiki_title) { 'page title test' }
......
......@@ -149,7 +149,7 @@ describe ProjectsController do
end
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
project.add_developer(user)
......
......@@ -67,7 +67,7 @@ describe SearchController do
using RSpec::Parameterized::TableSyntax
render_views
set(:project) { create(:project, :public, :repository, :wiki_repo) }
let_it_be(:project) { create(:project, :public, :repository, :wiki_repo) }
before do
expect(::Gitlab::GitalyClient).to receive(:allow_ref_name_caching).and_call_original
......
......@@ -10,6 +10,7 @@ require 'rspec/rails'
require 'shoulda/matchers'
require 'rspec/retry'
require 'rspec-parameterized'
require "test_prof/recipes/rspec/let_it_be"
rspec_profiling_is_configured =
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