Blob of rspec_on_rails/spec/rails/example/controller_spec_spec.rb (raw blob data)

1 require File.dirname(__FILE__) + '/../../spec_helper'
2 require 'controller_spec_controller'
3
4 ['integration', 'isolation'].each do |mode|
5 describe "A controller example running in #{mode} mode", :type => :controller do
6 controller_name :controller_spec
7 integrate_views if mode == 'integration'
8
9 it "should provide controller.session as session" do
10 get 'action_with_template'
11 session.should equal(controller.session)
12 end
13
14 it "should provide the same session object before and after the action" do
15 session_before = session
16 get 'action_with_template'
17 session.should equal(session_before)
18 end
19
20 it "should keep the same data in the session before and after the action" do
21 session[:foo] = :bar
22 get 'action_with_template'
23 session[:foo].should == :bar
24 end
25
26 it "should ensure controller.session is NOT nil before the action" do
27 controller.session.should_not be_nil
28 get 'action_with_template'
29 end
30
31 it "should ensure controller.session is NOT nil after the action" do
32 get 'action_with_template'
33 controller.session.should_not be_nil
34 end
35
36 it "should allow specifying a partial with partial name only" do
37 get 'action_with_partial'
38 response.should render_template("_partial")
39 end
40
41 it "should allow specifying a partial with expect_render" do
42 controller.expect_render(:partial => "controller_spec/partial")
43 get 'action_with_partial'
44 end
45
46 it "should allow specifying a partial with expect_render with object" do
47 controller.expect_render(:partial => "controller_spec/partial", :object => "something")
48 get 'action_with_partial_with_object', :thing => "something"
49 end
50
51 it "should allow specifying a partial with expect_render with locals" do
52 controller.expect_render(:partial => "controller_spec/partial", :locals => {:thing => "something"})
53 get 'action_with_partial_with_locals', :thing => "something"
54 end
55
56 it "should yield to render :update" do
57 template = stub("template")
58 controller.expect_render(:update).and_yield(template)
59 template.should_receive(:replace).with(:bottom, "replace_me", :partial => "non_existent_partial")
60 get 'action_with_render_update'
61 end
62
63 it "should allow a path relative to RAILS_ROOT/app/views/ when specifying a partial" do
64 get 'action_with_partial'
65 response.should render_template("controller_spec/_partial")
66 end
67
68 it "should provide access to flash" do
69 get 'action_which_sets_flash'
70 flash[:flash_key].should == "flash value"
71 end
72
73 it "should provide access to flash values set after a session reset" do
74 get 'action_setting_flash_after_session_reset'
75 flash[:after_reset].should == "available"
76 end
77
78 it "should not provide access to flash values set before a session reset" do
79 get 'action_setting_flash_before_session_reset'
80 flash[:before_reset].should_not == "available"
81 end
82
83 it "should provide access to session" do
84 session[:session_key] = "session value"
85 lambda do
86 get 'action_which_gets_session', :expected => "session value"
87 end.should_not raise_error
88 end
89
90 it "should support custom routes" do
91 route_for(:controller => "custom_route_spec", :action => "custom_route").should == "/custom_route"
92 end
93
94 it "should support existing routes" do
95 route_for(:controller => "controller_spec", :action => "some_action").should == "/controller_spec/some_action"
96 end
97
98 it "should generate params for custom routes" do
99 params_from(:get, '/custom_route').should == {:controller => "custom_route_spec", :action => "custom_route"}
100 end
101
102 it "should generate params for existing routes" do
103 params_from(:get, '/controller_spec/some_action').should == {:controller => "controller_spec", :action => "some_action"}
104 end
105
106 it "should expose instance vars through the assigns hash" do
107 get 'action_setting_the_assigns_hash'
108 assigns[:indirect_assigns_key].should == :indirect_assigns_key_value
109 end
110
111 it "should expose the assigns hash directly" do
112 get 'action_setting_the_assigns_hash'
113 assigns[:direct_assigns_key].should == :direct_assigns_key_value
114 end
115
116 it "should complain when calling should_receive(:render) on the controller" do
117 lambda {
118 controller.should_receive(:render)
119 }.should raise_error(RuntimeError, /should_receive\(:render\) has been disabled/)
120 end
121
122 it "should complain when calling stub!(:render) on the controller" do
123 lambda {
124 controller.stub!(:render)
125 }.should raise_error(RuntimeError, /stub!\(:render\) has been disabled/)
126 end
127
128 it "should NOT complain when calling should_receive with arguments other than :render" do
129 controller.should_receive(:anything_besides_render)
130 lambda {
131 controller.rspec_verify
132 }.should raise_error(Exception, /expected :anything_besides_render/)
133 end
134 end
135
136 describe "Given a controller spec for RedirectSpecController running in #{mode} mode", :type => :controller do
137 controller_name :redirect_spec
138 integrate_views if mode == 'integration'
139
140 it "a redirect should ignore the absence of a template" do
141 get 'action_with_redirect_to_somewhere'
142 response.should be_redirect
143 response.redirect_url.should == "http://test.host/redirect_spec/somewhere"
144 response.should redirect_to("http://test.host/redirect_spec/somewhere")
145 end
146
147 it "a call to response.should redirect_to should fail if no redirect" do
148 get 'action_with_no_redirect'
149 lambda {
150 response.redirect?.should be_true
151 }.should fail
152 lambda {
153 response.should redirect_to("http://test.host/redirect_spec/somewhere")
154 }.should fail_with("expected redirect to \"http://test.host/redirect_spec/somewhere\", got no redirect")
155 end
156 end
157
158 describe "Given a controller spec running in #{mode} mode" do
159 example_group = describe "A controller spec"
160 # , :type => :controller do
161 # integrate_views if mode == 'integration'
162 it "a spec in a context without controller_name set should fail with a useful warning" do
163 pending("need a new way to deal with examples that should_raise")
164 # ,
165 # :should_raise => [
166 # Spec::Expectations::ExpectationNotMetError,
167 # /You have to declare the controller name in controller specs/
168 # ] do
169 end
170 end
171
172 end
173
174 describe ControllerSpecController, :type => :controller do
175 it "should not require naming the controller if describe is passed a type" do
176 end
177 end
178
179 describe "A controller spec with controller_name set", :type => :controller do
180 controller_name :controller_spec
181
182 describe "nested" do
183 it "should inherit the controller name" do
184 get 'action_with_template'
185 response.should be_success
186 end
187 end
188 end
189
190 module Spec
191 module Rails
192 module Example
193 describe ControllerExampleGroup do
194 it "should clear its name from the description" do
195 group = describe("foo", :type => :controller) do
196 $nested_group = describe("bar") do
197 end
198 end
199 group.description.to_s.should == "foo"
200 $nested_group.description.to_s.should == "foo bar"
201 end
202 end
203 end
204 end
205 end