Skip to content

Commit

Permalink
Generators / tests: added support for RSpec
Browse files Browse the repository at this point in the history
Presenter generator invokes test framework to generate presenter tests.
  • Loading branch information
Alexander-Senko committed Oct 26, 2024
1 parent b5f649f commit 100ce27
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- `magic:presenter:install`
- `presenter`
- Test::Unit
- RSpec

### Fixed

Expand Down
7 changes: 7 additions & 0 deletions config/initializers/rspec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

RSpec.configure do |config|
config.include concern(:PresenterExampleGroup) {
included { metadata[:type] = :presenter }
}, file_path: %r'spec/presenters', type: :presenter
end if defined? RSpec
6 changes: 5 additions & 1 deletion lib/generators/presenter/USAGE
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Description:
CamelCased or under_scored, as an argument.

This generator invokes your configured test framework, which
defaults to TestUnit.
defaults to TestUnit. It supports both TestUnit and RSpec.

If a `--parent` option is given, it’s used as a superclass of the
created presenter.
Expand All @@ -15,10 +15,14 @@ Example:
Presenter: app/presenters/account_presenter.rb
for TestUnit:
Test: test/presenters/account_presenter_test.rb
for RSpec:
Spec: spec/presenters/account_presenter_spec.rb

bin/rails generate presenter admin/account

This will create:
Presenter: app/presenters/admin/account_presenter.rb
for TestUnit:
Test: test/presenters/admin/account_presenter_test.rb
for RSpec:
Spec: spec/presenters/admin/account_presenter_spec.rb
20 changes: 20 additions & 0 deletions lib/generators/rspec/presenter/presenter_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

require 'generators/rspec'
require 'generators/magic/presenter/generator'

module Rspec # :nodoc:
module Generators # :nodoc:
class PresenterGenerator < Base # :nodoc:
include Magic::Presenter::Generator

source_root File.expand_path('templates', __dir__)

cattr_reader :target_root, default: Pathname('spec')

def create_test_file
template 'presenter_spec.rb', "#{file_path}_spec.rb"
end
end
end
end
7 changes: 7 additions & 0 deletions lib/generators/rspec/presenter/templates/presenter_spec.rb.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'rails_helper'

<% module_namespacing do -%>
RSpec.describe <%= class_name %> do
pending "add some examples to (or delete) #{__FILE__}"
end
<% end -%>
20 changes: 20 additions & 0 deletions spec/generator/presenter_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,26 @@
it { is_expected.to be < ActiveSupport::TestCase }
end
end

context 'with RSpec' do
let(:options) { { test_framework: :rspec } }

let :path do
load file super() # the presenter should be defined before loading the spec

"spec/presenters/#{presenter_name.underscore}_spec.rb"
end

include_examples 'generates a valid file'

describe 'generated spec' do
subject { "RSpec::ExampleGroups::#{presenter_name.delete '::'}_3".safe_constantize } # HACK: name heuristics

it { is_expected.to be < RSpec::Core::ExampleGroup }
it { is_expected.to be < PresenterExampleGroup }
it { is_expected.to have_attributes metadata: include(type: :presenter) }
end
end
end
end

Expand Down

0 comments on commit 100ce27

Please sign in to comment.