Skip to content

Commit 7abbbfc

Browse files
committed
Documents how to use the new routes DSL with controller specs
1 parent 938d55c commit 7abbbfc

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

features/.nav

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- render_views.feature
1818
- anonymous_controller.feature
1919
- bypass_rescue.feature
20+
- engine_routes.feature
2021
- matchers:
2122
- new_record_matcher.feature
2223
- render_template_matcher.feature
@@ -37,3 +38,4 @@
3738
- route_to_matcher.feature
3839
- be_routable_matcher.feature
3940
- named_routes.feature
41+
- engine_routes.feature
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
Feature: engine routes for controllers
2+
3+
Controller specs can specify the routeset that will be used for the example
4+
group. This is most useful when testing Rails engines.
5+
6+
@unsupported-on-rails-3-0
7+
Scenario: specify engine route
8+
Given a file named "spec/controllers/widgets_controller_spec.rb" with:
9+
"""ruby
10+
require "spec_helper"
11+
12+
# A very simple Rails engine
13+
module MyEngine
14+
class Engine < ::Rails::Engine
15+
isolate_namespace MyEngine
16+
end
17+
18+
Engine.routes.draw do
19+
resources :widgets, :only => [:show] do
20+
get :random, :on => :collection
21+
end
22+
end
23+
24+
class WidgetsController < ::ActionController::Base
25+
def random
26+
@random_widget = Widget.all.shuffle.first
27+
redirect_to widget_path(@random_widget)
28+
end
29+
30+
def show
31+
@widget = Widget.find(params[:id])
32+
render :text => @widget.name
33+
end
34+
end
35+
end
36+
37+
describe MyEngine::WidgetsController do
38+
routes { MyEngine::Engine.routes }
39+
40+
it "redirects to a random widget" do
41+
widget1 = Widget.create!(:name => "Widget 1")
42+
widget2 = Widget.create!(:name => "Widget 2")
43+
44+
get :random
45+
expect(response).to be_redirect
46+
expect(response).to redirect_to(assigns(:random_widget))
47+
end
48+
end
49+
"""
50+
When I run `rspec spec`
51+
Then the examples should all pass

0 commit comments

Comments
 (0)