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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix with_routing test helper to make it work with api-only controllers #27371

Merged
merged 1 commit into from
Dec 29, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions actionpack/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* Make `with_routing` test helper work when testing controllers inheriting from `ActionController::API`

*Julia L贸pez*

* Use accept header in integration tests with `as: :json`

Instead of appending the `format` to the request path, Rails will figure
Expand Down
7 changes: 5 additions & 2 deletions actionpack/lib/action_dispatch/testing/assertions/routing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,11 @@ def with_routing
_routes = @routes

@controller.singleton_class.include(_routes.url_helpers)
@controller.view_context_class = Class.new(@controller.view_context_class) do
include _routes.url_helpers

if @controller.respond_to? :view_context_class
@controller.view_context_class = Class.new(@controller.view_context_class) do
include _routes.url_helpers
end
end
end
yield @routes
Expand Down
24 changes: 24 additions & 0 deletions actionpack/test/controller/action_pack_assertions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ def redirect_to_top_level_named_route
end
end

class ApiOnlyController < ActionController::API
def nothing
head :ok
end

def redirect_to_new_route
redirect_to new_route_url
end
end

class ActionPackAssertionsControllerTest < ActionController::TestCase
def test_render_file_absolute_path
get :render_file_absolute_path
Expand Down Expand Up @@ -170,6 +180,20 @@ def test_string_constraint
end
end

def test_with_routing_works_with_api_only_controllers
@controller = ApiOnlyController.new
Copy link
Contributor

Choose a reason for hiding this comment

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

Alternatively, I think we could skip the ApiOnlyController class with: @controller = Class.new(ActionController::Api).new


with_routing do |set|
set.draw do
get "new_route", to: "api_only#nothing"
get "redirect_to_new_route", to: "api_only#redirect_to_new_route"
end

process :redirect_to_new_route
assert_redirected_to "http://test.host/new_route"
end
end

def test_assert_redirect_to_named_route_failure
with_routing do |set|
set.draw do
Expand Down