Skip to content
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

Create controller generator for routing specs #2134

Merged
Merged
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
13 changes: 11 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"

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

def generate_routing_spec
return if actions.empty?
return unless options[:routing_specs]

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
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 do
run_generator %w(posts seek and destroy)
end
it { is_expected.not_to exist }
end

describe 'with --routing-specs flag' do
describe 'without action parameter' do
before do
run_generator %w(posts --routing-specs)
end
it { is_expected.not_to exist }
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 do
run_generator %w(posts seek and destroy --no-routing_specs)
end
it { is_expected.not_to exist }
end
end
end