Skip to content

Commit 2379212

Browse files
authored
Merge pull request #2372 from drwl/drwl/update-engine-generator
Add support for Rails engines routes in request specs
2 parents 5d76f46 + 384caac commit 2379212

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

features/request_specs/request_spec.feature

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,43 @@ Feature: request spec
112112
"""
113113
When I run `rspec spec/requests/widget_management_spec.rb`
114114
Then the example should pass
115+
116+
Scenario: using engine route helpers
117+
Given a file named "spec/requests/widgets_spec.rb" with:
118+
"""ruby
119+
require "rails_helper"
120+
121+
# A very simple Rails engine
122+
module MyEngine
123+
class Engine < ::Rails::Engine
124+
isolate_namespace MyEngine
125+
end
126+
127+
class LinksController < ::ActionController::Base
128+
def index
129+
render plain: 'hit_engine_route'
130+
end
131+
end
132+
end
133+
134+
MyEngine::Engine.routes.draw do
135+
resources :links, :only => [:index]
136+
end
137+
138+
Rails.application.routes.draw do
139+
mount MyEngine::Engine => "/my_engine"
140+
end
141+
142+
module MyEngine
143+
RSpec.describe "Links", :type => :request do
144+
include Engine.routes.url_helpers
145+
146+
it "redirects to a random widget" do
147+
get links_url
148+
expect(response.body).to eq('hit_engine_route')
149+
end
150+
end
151+
end
152+
"""
153+
When I run `rspec spec`
154+
Then the example should pass

lib/generators/rspec/scaffold/templates/request_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414

1515
<% module_namespacing do -%>
1616
RSpec.describe "/<%= name.underscore.pluralize %>", <%= type_metatag(:request) %> do
17+
<% if mountable_engine? -%>
18+
include Engine.routes.url_helpers
19+
<% end -%>
20+
1721
# <%= class_name %>. As you add validations to <%= class_name %>, be sure to
1822
# adjust the attributes here as well.
1923
let(:valid_attributes) {

spec/generators/rspec/scaffold/scaffold_generator_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@
3939
it { is_expected.to contain('renders a JSON response with the post') }
4040
it { is_expected.to contain('renders a JSON response with errors for the post') }
4141
end
42+
43+
describe 'in an engine' do
44+
before do
45+
allow_any_instance_of(::Rails::Generators::NamedBase).to receive(:mountable_engine?).and_return(true)
46+
run_generator %w[posts --request_specs]
47+
end
48+
49+
it { is_expected.to contain('Engine.routes.url_helpers') }
50+
end
4251
end
4352

4453
describe 'standard controller spec' do

0 commit comments

Comments
 (0)