From e697a1e6d05e6239a3a675572c5d5d0b46468ba3 Mon Sep 17 00:00:00 2001 From: "Andrew W. Lee" Date: Wed, 19 Aug 2020 22:26:58 -0700 Subject: [PATCH 1/6] Update request spec scaffold template Currently, when scaffolds are generated inside a Rails Engine, it generates requests specs that fail without modification. This happens because the Engine routes are not mounted under Rails.application. To fix this, Engine routes have to be included so that url and path helpers can be usable from within the spec file. --- lib/generators/rspec/scaffold/templates/request_spec.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/generators/rspec/scaffold/templates/request_spec.rb b/lib/generators/rspec/scaffold/templates/request_spec.rb index f68fe0f0fb..bc0719f64c 100644 --- a/lib/generators/rspec/scaffold/templates/request_spec.rb +++ b/lib/generators/rspec/scaffold/templates/request_spec.rb @@ -16,6 +16,11 @@ RSpec.describe "/<%= name.underscore.pluralize %>", <%= type_metatag(:request) %> do # <%= class_name %>. As you add validations to <%= class_name %>, be sure to # adjust the attributes here as well. + <% if mountable_engine? -%> + include Engine.routes.url_helpers + + <% end -%> + let(:valid_attributes) { skip("Add a hash of attributes valid for your model") } From e8161a71d8111922f174182a21069606d2b835aa Mon Sep 17 00:00:00 2001 From: "Andrew W. Lee" Date: Wed, 19 Aug 2020 22:41:03 -0700 Subject: [PATCH 2/6] Fix engine route include statement --- lib/generators/rspec/scaffold/templates/request_spec.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/generators/rspec/scaffold/templates/request_spec.rb b/lib/generators/rspec/scaffold/templates/request_spec.rb index bc0719f64c..4ce344e764 100644 --- a/lib/generators/rspec/scaffold/templates/request_spec.rb +++ b/lib/generators/rspec/scaffold/templates/request_spec.rb @@ -14,13 +14,12 @@ <% module_namespacing do -%> RSpec.describe "/<%= name.underscore.pluralize %>", <%= type_metatag(:request) %> do - # <%= class_name %>. As you add validations to <%= class_name %>, be sure to - # adjust the attributes here as well. <% if mountable_engine? -%> include Engine.routes.url_helpers - <% end -%> + # <%= class_name %>. As you add validations to <%= class_name %>, be sure to + # adjust the attributes here as well. let(:valid_attributes) { skip("Add a hash of attributes valid for your model") } From f04420744ae10997e88e82b2ce050185818aaa07 Mon Sep 17 00:00:00 2001 From: "Andrew W. Lee" Date: Tue, 25 Aug 2020 09:59:31 -0700 Subject: [PATCH 3/6] Add cucumber test for engine routes --- .../request_specs/engine_named_routes.feature | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 features/request_specs/engine_named_routes.feature diff --git a/features/request_specs/engine_named_routes.feature b/features/request_specs/engine_named_routes.feature new file mode 100644 index 0000000000..db42279c74 --- /dev/null +++ b/features/request_specs/engine_named_routes.feature @@ -0,0 +1,83 @@ +Feature: engine named routes + + Generated requeset specs inside a Rails Engine is able to use route and url helpers. + + Scenario: include engine route helpers + Given a file named "spec/requests/widgets_spec.rb" with: + """ruby + require "rails_helper" + + # A very simple Rails engine + module MyEngine + class Engine < ::Rails::Engine + isolate_namespace MyEngine + end + + class LinksController < ::ActionController::Base + def index + render plain: 'hit_engine_route' + end + end + end + + MyEngine::Engine.routes.draw do + resources :links, :only => [:index] + end + + Rails.application.routes.draw do + mount MyEngine::Engine => "/my_engine" + end + + module MyEngine + RSpec.describe "Links", :type => :request do + + include Engine.routes.url_helpers + + it "redirects to a random widget" do + get links_url + expect(response.body).to eq('hit_engine_route') + end + end + end + """ + When I run `rspec spec` + Then the example should pass + + Scenario: do not include engine route helpers + Given a file named "spec/requests/widgets_spec.rb" with: + """ruby + require "rails_helper" + + # A very simple Rails engine + module MyEngine + class Engine < ::Rails::Engine + isolate_namespace MyEngine + end + + class LinksController < ::ActionController::Base + def index + render plain: 'hit_engine_route' + end + end + end + + MyEngine::Engine.routes.draw do + resources :links, :only => [:index] + end + + Rails.application.routes.draw do + mount MyEngine::Engine => "/my_engine" + end + + module MyEngine + RSpec.describe "Links", :type => :request do + + it "redirects to a random widget" do + get links_url + expect(response.body).to eq('hit_engine_route') + end + end + end + """ + When I run `rspec spec` + Then the example should fail \ No newline at end of file From b634f350eb754f61d125d7cdd1fc61cf232cc42e Mon Sep 17 00:00:00 2001 From: "Andrew W. Lee" Date: Wed, 26 Aug 2020 09:01:55 -0700 Subject: [PATCH 4/6] Update feature test with PR feedback --- .../request_specs/engine_named_routes.feature | 44 +------------------ 1 file changed, 2 insertions(+), 42 deletions(-) diff --git a/features/request_specs/engine_named_routes.feature b/features/request_specs/engine_named_routes.feature index db42279c74..2679a8fbcd 100644 --- a/features/request_specs/engine_named_routes.feature +++ b/features/request_specs/engine_named_routes.feature @@ -1,8 +1,8 @@ Feature: engine named routes - Generated requeset specs inside a Rails Engine is able to use route and url helpers. + Request specs inside a Rails Engine can include the engines routes to use url helpers. - Scenario: include engine route helpers + Scenario: using engine route helpers Given a file named "spec/requests/widgets_spec.rb" with: """ruby require "rails_helper" @@ -30,7 +30,6 @@ Feature: engine named routes module MyEngine RSpec.describe "Links", :type => :request do - include Engine.routes.url_helpers it "redirects to a random widget" do @@ -42,42 +41,3 @@ Feature: engine named routes """ When I run `rspec spec` Then the example should pass - - Scenario: do not include engine route helpers - Given a file named "spec/requests/widgets_spec.rb" with: - """ruby - require "rails_helper" - - # A very simple Rails engine - module MyEngine - class Engine < ::Rails::Engine - isolate_namespace MyEngine - end - - class LinksController < ::ActionController::Base - def index - render plain: 'hit_engine_route' - end - end - end - - MyEngine::Engine.routes.draw do - resources :links, :only => [:index] - end - - Rails.application.routes.draw do - mount MyEngine::Engine => "/my_engine" - end - - module MyEngine - RSpec.describe "Links", :type => :request do - - it "redirects to a random widget" do - get links_url - expect(response.body).to eq('hit_engine_route') - end - end - end - """ - When I run `rspec spec` - Then the example should fail \ No newline at end of file From 46331331728d785ee231bef35b982a7cd70f7c85 Mon Sep 17 00:00:00 2001 From: "Andrew W. Lee" Date: Wed, 26 Aug 2020 09:04:43 -0700 Subject: [PATCH 5/6] Move feature test into request_spec file --- .../request_specs/engine_named_routes.feature | 43 ------------------- features/request_specs/request_spec.feature | 40 +++++++++++++++++ 2 files changed, 40 insertions(+), 43 deletions(-) delete mode 100644 features/request_specs/engine_named_routes.feature diff --git a/features/request_specs/engine_named_routes.feature b/features/request_specs/engine_named_routes.feature deleted file mode 100644 index 2679a8fbcd..0000000000 --- a/features/request_specs/engine_named_routes.feature +++ /dev/null @@ -1,43 +0,0 @@ -Feature: engine named routes - - Request specs inside a Rails Engine can include the engines routes to use url helpers. - - Scenario: using engine route helpers - Given a file named "spec/requests/widgets_spec.rb" with: - """ruby - require "rails_helper" - - # A very simple Rails engine - module MyEngine - class Engine < ::Rails::Engine - isolate_namespace MyEngine - end - - class LinksController < ::ActionController::Base - def index - render plain: 'hit_engine_route' - end - end - end - - MyEngine::Engine.routes.draw do - resources :links, :only => [:index] - end - - Rails.application.routes.draw do - mount MyEngine::Engine => "/my_engine" - end - - module MyEngine - RSpec.describe "Links", :type => :request do - include Engine.routes.url_helpers - - it "redirects to a random widget" do - get links_url - expect(response.body).to eq('hit_engine_route') - end - end - end - """ - When I run `rspec spec` - Then the example should pass diff --git a/features/request_specs/request_spec.feature b/features/request_specs/request_spec.feature index 5be1b37f23..f832d2845a 100644 --- a/features/request_specs/request_spec.feature +++ b/features/request_specs/request_spec.feature @@ -112,3 +112,43 @@ Feature: request spec """ When I run `rspec spec/requests/widget_management_spec.rb` Then the example should pass + + Scenario: using engine route helpers + Given a file named "spec/requests/widgets_spec.rb" with: + """ruby + require "rails_helper" + + # A very simple Rails engine + module MyEngine + class Engine < ::Rails::Engine + isolate_namespace MyEngine + end + + class LinksController < ::ActionController::Base + def index + render plain: 'hit_engine_route' + end + end + end + + MyEngine::Engine.routes.draw do + resources :links, :only => [:index] + end + + Rails.application.routes.draw do + mount MyEngine::Engine => "/my_engine" + end + + module MyEngine + RSpec.describe "Links", :type => :request do + include Engine.routes.url_helpers + + it "redirects to a random widget" do + get links_url + expect(response.body).to eq('hit_engine_route') + end + end + end + """ + When I run `rspec spec` + Then the example should pass From 384caac01c6340bbd3427bf31babcf1c93450c73 Mon Sep 17 00:00:00 2001 From: "Andrew W. Lee" Date: Wed, 26 Aug 2020 10:13:24 -0700 Subject: [PATCH 6/6] Add spec for including engine routes --- .../generators/rspec/scaffold/scaffold_generator_spec.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/spec/generators/rspec/scaffold/scaffold_generator_spec.rb b/spec/generators/rspec/scaffold/scaffold_generator_spec.rb index e9fe88ce96..a56b5858ea 100644 --- a/spec/generators/rspec/scaffold/scaffold_generator_spec.rb +++ b/spec/generators/rspec/scaffold/scaffold_generator_spec.rb @@ -39,6 +39,15 @@ it { is_expected.to contain('renders a JSON response with the post') } it { is_expected.to contain('renders a JSON response with errors for the post') } end + + describe 'in an engine' do + before do + allow_any_instance_of(::Rails::Generators::NamedBase).to receive(:mountable_engine?).and_return(true) + run_generator %w[posts --request_specs] + end + + it { is_expected.to contain('Engine.routes.url_helpers') } + end end describe 'standard controller spec' do