Skip to content

Add support for Rails engines routes in request specs #2372

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

Merged
merged 6 commits into from
Aug 27, 2020
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
40 changes: 40 additions & 0 deletions features/request_specs/request_spec.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions lib/generators/rspec/scaffold/templates/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

<% module_namespacing do -%>
RSpec.describe "/<%= name.underscore.pluralize %>", <%= type_metatag(:request) %> do
<% 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) {
Expand Down
9 changes: 9 additions & 0 deletions spec/generators/rspec/scaffold/scaffold_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down