changelog shortlog tags changeset manifest revisions annotate raw

vendor/plugins/rspec_on_rails/spec/rails/example/controller_spec_spec.rb

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