-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13739 from opf/code-maintenance/add-rspec-service…
…-spec-generator Create RSpec "service" spec generator
- Loading branch information
Showing
4 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
Description: | ||
Creates a new service spec file for the given SERVICE_NAME | ||
in the specified MODULE_NAME (if given). Otherwise, creates | ||
the service spec file in the root /specs directory. | ||
|
||
Example: | ||
bin/rails generate open_project:rspec:service Some::Service | ||
|
||
This will create: | ||
/specs/services/some/service_spec.rb | ||
|
||
Example: | ||
bin/rails generate open_project:rspec:service Some::Service -m meetings | ||
|
||
This will create: | ||
/modules/meetings/specs/services/some/service_spec.rb |
73 changes: 73 additions & 0 deletions
73
lib/generators/open_project/rspec/service/service_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# frozen_string_literal: true | ||
|
||
# -- copyright | ||
# OpenProject is an open source project management software. | ||
# Copyright (C) 2023 the OpenProject GmbH | ||
# | ||
# This program is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU General Public License version 3. | ||
# | ||
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: | ||
# Copyright (C) 2006-2013 Jean-Philippe Lang | ||
# Copyright (C) 2010-2013 the ChiliProject Team | ||
# | ||
# This program is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU General Public License | ||
# as published by the Free Software Foundation; either version 2 | ||
# of the License, or (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program; if not, write to the Free Software | ||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
# | ||
# See COPYRIGHT and LICENSE files for more details. | ||
# ++ | ||
|
||
module OpenProject | ||
module Rspec | ||
module Generators | ||
class ServiceGenerator < Rails::Generators::Base | ||
source_root File.expand_path("templates", __dir__) | ||
|
||
argument :service_name, | ||
type: :string, | ||
required: true, | ||
desc: 'Constant of the service the spec is being generated for' | ||
|
||
class_option :module_name, | ||
aliases: %i[m], | ||
type: :string, | ||
optional: true, | ||
desc: 'Module to generate the service spec file for' | ||
|
||
def generate_service_spec | ||
template 'service_spec.rb', file_path | ||
end | ||
|
||
private | ||
|
||
def file_path | ||
namespace = service_name.deconstantize.underscore | ||
file_name = "#{service_name.demodulize.underscore}_spec.rb" | ||
|
||
Rails.root.join(service_specs_root_directory, | ||
namespace, | ||
file_name) | ||
end | ||
|
||
def service_specs_root_directory | ||
if options[:module_name] | ||
"modules/#{options[:module_name]}/spec/services" | ||
else | ||
"spec/services" | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
71 changes: 71 additions & 0 deletions
71
lib/generators/open_project/rspec/service/templates/service_spec.rb.tt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# frozen_string_literal: true | ||
|
||
# -- copyright | ||
# OpenProject is an open source project management software. | ||
# Copyright (C) 2023 the OpenProject GmbH | ||
# | ||
# This program is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU General Public License version 3. | ||
# | ||
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: | ||
# Copyright (C) 2006-2013 Jean-Philippe Lang | ||
# Copyright (C) 2010-2013 the ChiliProject Team | ||
# | ||
# This program is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU General Public License | ||
# as published by the Free Software Foundation; either version 2 | ||
# of the License, or (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program; if not, write to the Free Software | ||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
# | ||
# See COPYRIGHT and LICENSE files for more details. | ||
# ++ | ||
|
||
require 'spec_helper' | ||
# Require the file for the BaseServices shared example you're keeping | ||
# | ||
# require 'services/base_services/behaves_like_create_service' | ||
# require 'services/base_services/behaves_like_update_service' | ||
# require 'services/base_services/behaves_like_delete_service' | ||
|
||
RSpec.describe <%= service_name %> do | ||
# Keep the BaseServices shared_example that applies from below | ||
# | ||
# it_behaves_like 'BaseServices create service' | ||
# it_behaves_like 'BaseServices update service' | ||
# it_behaves_like 'BaseService delete service' | ||
|
||
# AND/OR build your own | ||
# Here's something to get started with | ||
shared_examples 'is success' do | ||
it { is_expected.to be_success } | ||
end | ||
|
||
shared_examples 'is failure' do | ||
it { is_expected.to be_failure } | ||
end | ||
|
||
let(:user) { build_stubbed(:user) } | ||
let(:instance) { described_class.new(user:) } | ||
|
||
subject(:service_call) { instance.call(params) } | ||
|
||
describe '#call' do | ||
context 'with valid params' do | ||
let(:params) { raise 'Define me' } | ||
|
||
it_behaves_like 'is success' | ||
end | ||
|
||
context 'with invalid params' do | ||
it_behaves_like 'is failure' | ||
end | ||
end | ||
end |