Skip to content

Create controller generator for routing specs #1806

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/generators/rspec/controller/controller_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ class ControllerGenerator < Base
argument :actions, :type => :array, :default => [], :banner => "action action"

class_option :template_engine, :desc => "Template engine to generate view files"
class_option :controller_specs, :type => :boolean, :default => true
class_option :view_specs, :type => :boolean, :default => true
class_option :controller_specs, :type => :boolean, :default => true, :desc => "Generate controller specs"
class_option :view_specs, :type => :boolean, :default => true, :desc => "Generate view specs"
class_option :routing_specs, :type => :boolean, :default => false, :desc => "Generate routing specs"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double spacing here, unless you were intending to align the :desc, in which case the latst needs a space removed.


def generate_controller_spec
return unless options[:controller_specs]
Expand All @@ -29,6 +30,13 @@ def generate_view_specs
File.join("spec", "views", file_path, "#{@action}.html.#{options[:template_engine]}_spec.rb")
end
end

def generate_routing_spec
return unless options[:routing_specs]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably return is actions is empty like views...

template 'routing_spec.rb',
File.join('spec/routing', class_path, "#{file_name}_routing_spec.rb")
end
end
end
end
13 changes: 13 additions & 0 deletions lib/generators/rspec/controller/templates/routing_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'rails_helper'

<% module_namespacing do -%>
RSpec.describe <%= class_name %>Controller, <%= type_metatag(:routing) %> do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This describe should be a doc string, we don't need the constant here.

describe 'routing' do
<% for action in actions -%>
it 'routes to #<%= action %>' do
expect(:get => "/<%= class_name.underscore %>/<%= action %>").to route_to("<%= class_name.underscore %>#<%= action %>")
end
<% end -%>
end
end
<% end -%>
37 changes: 37 additions & 0 deletions spec/generators/rspec/controller/controller_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,41 @@
end
end
end

describe 'routing spec' do
subject { file('spec/routing/posts_routing_spec.rb') }

describe 'with no flag' do
before { run_generator %w(posts seek and destroy) }

it { is_expected.not_to exist }
end

describe 'with --routing-specs flag' do
describe 'without action parameter' do
before { run_generator %w(posts --routing-specs) }

it { is_expected.to contain(/require 'rails_helper'/) }
it { is_expected.to contain(/^RSpec.describe PostsController, #{type_metatag(:routing)}/) }
it { is_expected.to contain(/describe 'routing'/) }
end

describe 'with action parameter' do
before { run_generator %w(posts seek --routing-specs) }

it { is_expected.to contain(/require 'rails_helper'/) }
it { is_expected.to contain(/^RSpec.describe PostsController, #{type_metatag(:routing)}/) }
it { is_expected.to contain(/describe 'routing'/) }
``
it { is_expected.to contain(/it 'routes to #seek'/) }
it { is_expected.to contain(/expect\(:get => "\/posts\/seek"\).to route_to\("posts#seek"\)/) }
end
end

describe 'with --no-routing-specs flag' do
before { run_generator %w(posts seek and destroy --no-routing_specs) }

it { is_expected.not_to exist }
end
end
end