Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Make spec faster

Christophe Bliard22d804c2102f
diff --git a/modules/backlogs/spec/contracts/work_packages/base_contract_spec.rb b/modules/backlogs/spec/contracts/work_packages/base_contract_spec.rb
index 2e0b6ba5218..56e94aadf3a 100644
--- a/modules/backlogs/spec/contracts/work_packages/base_contract_spec.rb
+++ b/modules/backlogs/spec/contracts/work_packages/base_contract_spec.rb
@@ -29,48 +29,28 @@
require 'spec_helper'
RSpec.describe WorkPackages::BaseContract, type: :model do
+ shared_let(:type_feature) { create(:type_feature) }
+ shared_let(:type_task) { create(:type_task) }
+ shared_let(:type_bug) { create(:type_bug) }
+ shared_let(:backlogs_types) { [type_feature, type_task, type_bug] }
+ shared_let(:project) do
+ create(:project, types: backlogs_types)
+ end
+ shared_let(:other_project) do
+ create(:project, types: backlogs_types)
+ end
+ shared_let(:user) do
+ create(:admin, member_with_roles: { project => create(:project_role),
+ other_project => create(:project_role) })
+ end
+
let(:instance) { described_class.new(work_package, user) }
- let(:type_feature) { build(:type_feature) }
- let(:type_task) { build(:type_task) }
- let(:type_bug) { build(:type_bug) }
let(:version1) { build_stubbed(:version, name: 'Version1', project: p) }
let(:version2) { build_stubbed(:version, name: 'Version2', project: p) }
- let(:role) { build(:project_role) }
- let(:user) { build(:admin) }
+
let(:issue_priority) { build(:priority) }
let(:status) { build_stubbed(:status, name: 'status 1', is_default: true) }
- let(:project) do
- p = build(:project, members: [build(:member,
- principal: user,
- roles: [role])],
- types: [type_feature, type_task, type_bug])
-
- allow(p)
- .to receive(:assignable_versions)
- .and_return([version1,
- version2])
-
- p.enabled_module_names += ['backlogs']
-
- p
- end
-
- let(:other_project) do
- p = build(:project, members: [build(:member,
- principal: user,
- roles: [role])],
- types: [type_feature, type_task, type_bug])
-
- allow(p)
- .to receive(:assignable_versions)
- .and_return([version1,
- version2])
- p.enabled_module_names += ['backlogs']
-
- p
- end
-
let(:story) do
build_stubbed(:work_package,
subject: 'Story',
@@ -141,12 +121,8 @@ RSpec.describe WorkPackages::BaseContract, type: :model do
scope = instance_double(ActiveRecord::Relation)
allow(scope)
- .to receive(:where)
- .and_return(scope)
-
- allow(scope)
- .to receive(:empty?)
- .and_return(false)
+ .to receive_messages(where: scope,
+ empty?: false)
scope
end
@@ -154,7 +130,12 @@ RSpec.describe WorkPackages::BaseContract, type: :model do
subject(:valid) { instance.validate }
before do
- project.save!
+ allow(project)
+ .to receive(:assignable_versions)
+ .and_return([version1, version2])
+ allow(other_project)
+ .to receive(:assignable_versions)
+ .and_return([version1, version2])
allow(WorkPackage)
.to receive(:relatable)
@@ -183,7 +164,7 @@ RSpec.describe WorkPackages::BaseContract, type: :model do
shared_examples_for 'version being restricted by the parent' do
before do
- work_package.parent = parent if work_package.parent.blank?
+ work_package.parent ||= parent
end
describe 'WITHOUT a version and the parent also having no version' do
@@ -192,7 +173,7 @@ RSpec.describe WorkPackages::BaseContract, type: :model do
work_package.version = nil
end
- it_behaves_like 'is valid'
+ include_examples 'is valid'
end
describe 'WITHOUT a version and the parent having a version' do
@@ -201,7 +182,7 @@ RSpec.describe WorkPackages::BaseContract, type: :model do
work_package.version = nil
end
- it_behaves_like 'is invalid and notes the error'
+ include_examples 'is invalid and notes the error'
end
describe 'WITH a version and the parent having a different version' do
@@ -210,7 +191,7 @@ RSpec.describe WorkPackages::BaseContract, type: :model do
work_package.version = version2
end
- it_behaves_like 'is invalid and notes the error'
+ include_examples 'is invalid and notes the error'
end
describe 'WITH a version and the parent having the same version' do
@@ -219,7 +200,7 @@ RSpec.describe WorkPackages::BaseContract, type: :model do
work_package.version = version1
end
- it_behaves_like 'is valid'
+ include_examples 'is valid'
end
describe 'WITH a version and the parent having no version' do
@@ -228,13 +209,13 @@ RSpec.describe WorkPackages::BaseContract, type: :model do
work_package.version = version1
end
- it_behaves_like 'is invalid and notes the error'
+ include_examples 'is invalid and notes the error'
end
end
shared_examples_for 'version not being restricted by the parent' do
before do
- work_package.parent = parent if work_package.parent.blank?
+ work_package.parent ||= parent
end
describe 'WITHOUT a version and the parent also having no version' do
@@ -243,7 +224,7 @@ RSpec.describe WorkPackages::BaseContract, type: :model do
work_package.version = nil
end
- it_behaves_like 'is valid'
+ include_examples 'is valid'
end
describe 'WITHOUT a version and the parent having a version' do
@@ -252,7 +233,7 @@ RSpec.describe WorkPackages::BaseContract, type: :model do
work_package.version = nil
end
- it_behaves_like 'is valid'
+ include_examples 'is valid'
end
describe 'WITH a version and the parent having a different version' do
@@ -261,7 +242,7 @@ RSpec.describe WorkPackages::BaseContract, type: :model do
work_package.version = version2
end
- it_behaves_like 'is valid'
+ include_examples 'is valid'
end
describe 'WITH a version and the parent having the same version' do
@@ -270,7 +251,7 @@ RSpec.describe WorkPackages::BaseContract, type: :model do
work_package.version = version1
end
- it_behaves_like 'is valid'
+ include_examples 'is valid'
end
describe 'WITH a version and the parent having no version' do
@@ -279,7 +260,7 @@ RSpec.describe WorkPackages::BaseContract, type: :model do
work_package.version = version1
end
- it_behaves_like 'is valid'
+ include_examples 'is valid'
end
end
@@ -289,7 +270,7 @@ RSpec.describe WorkPackages::BaseContract, type: :model do
work_package.version = nil
end
- it_behaves_like 'is valid'
+ include_examples 'is valid'
end
describe 'WITH a version' do
@@ -297,7 +278,7 @@ RSpec.describe WorkPackages::BaseContract, type: :model do
work_package.version = version1
end
- it_behaves_like 'is valid'
+ include_examples 'is valid'
end
end
@@ -335,13 +316,13 @@ RSpec.describe WorkPackages::BaseContract, type: :model do
let(:parent) { task2 }
- it_behaves_like 'version being restricted by the parent'
+ include_examples 'version being restricted by the parent'
end
describe "WITH a story as its parent" do
let(:parent) { story }
- it_behaves_like 'version being restricted by the parent'
+ include_examples 'version being restricted by the parent'
end
describe "WITH a non backlogs tracked work_package as its parent" do
@@ -386,7 +367,7 @@ RSpec.describe WorkPackages::BaseContract, type: :model do
work_package.parent = parent
end
- it_behaves_like 'is valid'
+ include_examples 'is valid'
end
describe 'WITH the work_package having a different project' do
@@ -395,7 +376,7 @@ RSpec.describe WorkPackages::BaseContract, type: :model do
work_package.project = other_project
end
- it_behaves_like 'is valid'
+ include_examples 'is valid'
end
end
@@ -405,13 +386,13 @@ RSpec.describe WorkPackages::BaseContract, type: :model do
describe 'WITH a story as its parent' do
let(:parent) { story }
- it_behaves_like 'project id unrestricted by parent'
+ include_examples 'project id unrestricted by parent'
end
describe 'WITH a non backlogs work package as its parent' do
let(:parent) { bug }
- it_behaves_like 'project id unrestricted by parent'
+ include_examples 'project id unrestricted by parent'
end
end
@@ -421,13 +402,13 @@ RSpec.describe WorkPackages::BaseContract, type: :model do
describe 'WITH a story as its parent' do
let(:parent) { story2 }
- it_behaves_like 'project id unrestricted by parent'
+ include_examples 'project id unrestricted by parent'
end
describe 'WITH a non backlogs work package as its parent' do
let(:parent) { bug }
- it_behaves_like 'project id unrestricted by parent'
+ include_examples 'project id unrestricted by parent'
end
end
@@ -437,13 +418,13 @@ RSpec.describe WorkPackages::BaseContract, type: :model do
describe 'WITH a story as its parent' do
let(:parent) { story }
- it_behaves_like 'project id unrestricted by parent'
+ include_examples 'project id unrestricted by parent'
end
describe 'WITH a non backlogs work package as its parent' do
let(:parent) { bug2 }
- it_behaves_like 'project id unrestricted by parent'
+ include_examples 'project id unrestricted by parent'
end
end
end