Skip to content
This repository
Newer
Older
100644 244 lines (208 sloc) 8.458 kb
6d673c20 »
2007-11-26 Made the following renames:
1 module Spec
2 module Rails
3 module Example
4 # Controller Examples live in $RAILS_ROOT/spec/controllers/.
5 #
6853e88c »
2009-02-13 general cleanup
6 # Controller Examples use Spec::Rails::Example::ControllerExampleGroup,
7 # which supports running specs for Controllers in two modes, which
8 # represent the tension between the more granular testing common in TDD
9 # and the more high level testing built into rails. BDD sits somewhere
10 # in between: we want to a balance between specs that are close enough
11 # to the code to enable quick fault isolation and far enough away from
12 # the code to enable refactoring with minimal changes to the existing
13 # specs.
6d673c20 »
2007-11-26 Made the following renames:
14 #
15 # == Isolation mode (default)
16 #
6853e88c »
2009-02-13 general cleanup
17 # No dependencies on views because none are ever rendered. The benefit
18 # of this mode is that can spec the controller completely independent of
19 # the view, allowing that responsibility to be handled later, or by
20 # somebody else. Combined w/ separate view specs, this also provides
21 # better fault isolation.
6d673c20 »
2007-11-26 Made the following renames:
22 #
23 # == Integration mode
24 #
25 # To run in this mode, include the +integrate_views+ declaration
26 # in your controller context:
27 #
28 # describe ThingController do
29 # integrate_views
30 # ...
31 #
6853e88c »
2009-02-13 general cleanup
32 # In this mode, controller specs are run in the same way that rails
33 # functional tests run - one set of tests for both the controllers and
34 # the views. The benefit of this approach is that you get wider coverage
35 # from each spec. Experienced rails developers may find this an easier
36 # approach to begin with, however we encourage you to explore using the
37 # isolation mode and revel in its benefits.
6d673c20 »
2007-11-26 Made the following renames:
38 #
39 # == Expecting Errors
40 #
e98e0c7f »
2009-01-10 Add failing code examples to show that errors captured by rescue_from
41 # Rspec on Rails will raise errors that occur in controller actions and
42 # are not rescued or handeled with rescue_from.
6d673c20 »
2007-11-26 Made the following renames:
43 #
44 class ControllerExampleGroup < FunctionalExampleGroup
45 class << self
c3321acb »
2007-12-07 better examples (and a better fix) for http://rspec.lighthouseapp.com…
46
6853e88c »
2009-02-13 general cleanup
47 # Use integrate_views to instruct RSpec to render views in
48 # your controller examples in Integration mode.
6d673c20 »
2007-11-26 Made the following renames:
49 #
50 # describe ThingController do
51 # integrate_views
52 # ...
53 #
6853e88c »
2009-02-13 general cleanup
54 # See Spec::Rails::Example::ControllerExampleGroup for more
55 # information about Integration and Isolation modes.
4df2c721 »
2008-03-16 got nested integrate_views working
56 def integrate_views(integrate_views = true)
57 @integrate_views = integrate_views
6d673c20 »
2007-11-26 Made the following renames:
58 end
4df2c721 »
2008-03-16 got nested integrate_views working
59
6d673c20 »
2007-11-26 Made the following renames:
60 def integrate_views? # :nodoc:
61 @integrate_views
62 end
4df2c721 »
2008-03-16 got nested integrate_views working
63
64 def inherited(klass) # :nodoc:
65 klass.integrate_views(integrate_views?)
5d114bee »
2009-02-07 Changed ControllerExampleGroup to use controller as the implicit subj…
66 klass.subject { controller }
4df2c721 »
2008-03-16 got nested integrate_views working
67 super
68 end
98f7d8a2 »
2009-02-08 cleaning up example group classes - remove controller_class_name
69
6853e88c »
2009-02-13 general cleanup
70 def set_description(*args) # :nodoc:
98f7d8a2 »
2009-02-08 cleaning up example group classes - remove controller_class_name
71 super
72 if described_class && described_class.ancestors.include?(ActionController::Base)
73 tests described_class
74 end
75 end
6d673c20 »
2007-11-26 Made the following renames:
76
6853e88c »
2009-02-13 general cleanup
77 # When you don't pass a controller to describe, like this:
78 #
79 # describe ThingsController do
80 #
81 # ... then you must provide a controller_name within the context of
6d673c20 »
2007-11-26 Made the following renames:
82 # your controller specs:
83 #
84 # describe "ThingController" do
85 # controller_name :thing
86 # ...
87 def controller_name(name)
98f7d8a2 »
2009-02-08 cleaning up example group classes - remove controller_class_name
88 tests "#{name}_controller".camelize.constantize
6d673c20 »
2007-11-26 Made the following renames:
89 end
90 end
9fd1279b »
2009-01-18 Derive example group classes from the rails test cases
91
6d673c20 »
2007-11-26 Made the following renames:
92 before(:each) do
93 # Some Rails apps explicitly disable ActionMailer in environment.rb
94 if defined?(ActionMailer)
95 @deliveries = []
96 ActionMailer::Base.deliveries = @deliveries
97 end
98
99 unless @controller.class.ancestors.include?(ActionController::Base)
6853e88c »
2009-02-13 general cleanup
100 Spec::Expectations.fail_with <<-MESSAGE
101 Controller specs need to know what controller is being specified. You can
102 indicate this by passing the controller to describe():
103
104 describe MyController do
105
106 or by declaring the controller's name
107
108 describe "a MyController" do
109 controller_name :my #invokes the MyController
110 end
111 MESSAGE
6d673c20 »
2007-11-26 Made the following renames:
112 end
6853e88c »
2009-02-13 general cleanup
113 @controller.extend ControllerInstanceMethods
6d673c20 »
2007-11-26 Made the following renames:
114 @controller.integrate_views! if @integrate_views
115 @controller.session = session
116 end
117
118 attr_reader :response, :request, :controller
119
d7c0773a »
2008-10-06 add options hash for ExampleGroupMethods.initialize
120 def initialize(defined_description, options={}, &implementation) #:nodoc:
6d673c20 »
2007-11-26 Made the following renames:
121 super
122 @integrate_views = self.class.integrate_views?
123 end
939d7454 »
2009-01-18 Delegate route_for to assert_recognizes.
124
40ce2c35 »
2009-02-15 Add bypass_rescue for controller specs
125 # Bypasses any error rescues defined with rescue_from. Useful
126 # in cases in which you want to specify errors coming out of
127 # actions that might be caught by a rescue_from clause that is
128 # specified separately.
129 #
130 # Note that this will override the effect of rescue_action_in_public
131 def bypass_rescue
132 if ::Rails::VERSION::STRING >= '2.2'
133 def controller.rescue_action(exception)
134 raise exception
135 end
136 else
137 def controller.rescue_action_with_handler(exception)
138 raise exception
139 end
140 end
141 end
142
de905337 »
2009-01-19 refactor BEFORE running the full build, remember?
143 protected
144
145 def _assigns_hash_proxy
6853e88c »
2009-02-13 general cleanup
146 @_assigns_hash_proxy ||= AssignsHashProxy.new(self) {@response.template}
de905337 »
2009-01-19 refactor BEFORE running the full build, remember?
147 end
148
149 private
b578d767 »
2009-02-12 clean up template extensions related to isolation
150
151 module TemplateIsolationExtensions
152 def file_exists?(ignore); true; end
153
154 def render_file(*args)
155 @first_render ||= args[0] unless args[0] =~ /^layouts/
156 end
157
158 # Rails 2.2
159 def _pick_template(*args)
160 @_first_render ||= args[0] unless args[0] =~ /^layouts/
161 PickedTemplate.new
162 end
163
164 def render(*args)
d84ed349 »
2009-03-06 Fixed bug where render :inline didn't render without integrate_views
165 return super if Hash === args.last && args.last[:inline]
6853e88c »
2009-02-13 general cleanup
166 @_rendered ? record_render(args[0]) : super
167 end
168
169 private
170
171 def record_render(opts)
172 (@_rendered[:template] ||= opts[:file]) if opts[:file]
173 (@_rendered[:partials][opts[:partial]] += 1) if opts[:partial]
174 end
175
176 # Returned by _pick_template when running controller examples in isolation mode.
177 class PickedTemplate
178 # Do nothing when running controller examples in isolation mode.
179 def render_template(*ignore_args); end
180 # Do nothing when running controller examples in isolation mode.
181 def render_partial(*ignore_args); end
b578d767 »
2009-02-12 clean up template extensions related to isolation
182 end
183 end
6853e88c »
2009-02-13 general cleanup
184
185 module ControllerInstanceMethods # :nodoc:
6d673c20 »
2007-11-26 Made the following renames:
186 include Spec::Rails::Example::RenderObserver
187
b578d767 »
2009-02-12 clean up template extensions related to isolation
188 # === render(options = nil, extra_options={}, &block)
6d673c20 »
2007-11-26 Made the following renames:
189 #
190 # This gets added to the controller's singleton meta class,
191 # allowing Controller Examples to run in two modes, freely switching
6853e88c »
2009-02-13 general cleanup
192 # from example group to example group.
b702dfbb »
2009-02-11 support 2 arg version of ActionController::Base#render
193 def render(options=nil, extra_options={}, &block)
6d673c20 »
2007-11-26 Made the following renames:
194 unless block_given?
195 unless integrate_views?
b578d767 »
2009-02-12 clean up template extensions related to isolation
196 @template.extend TemplateIsolationExtensions
6d673c20 »
2007-11-26 Made the following renames:
197 end
198 end
199
42cb7cea »
2008-01-25 Pass block to mock_proxy if and_yield is used in expect_render on a c…
200 if matching_message_expectation_exists(options)
eb53fc0e »
2008-09-28 deprecate expect_render and stub_render in favor of rails-specific ex…
201 render_proxy.render(options, &block)
6d673c20 »
2007-11-26 Made the following renames:
202 @performed_render = true
203 else
f909c0d9 »
2008-08-23 got rspec-rails working against edge rails (as of e48e77e on 8/18/200…
204 if matching_stub_exists(options)
205 @performed_render = true
206 else
b702dfbb »
2009-02-11 support 2 arg version of ActionController::Base#render
207 super
6d673c20 »
2007-11-26 Made the following renames:
208 end
209 end
210 end
42cb7cea »
2008-01-25 Pass block to mock_proxy if and_yield is used in expect_render on a c…
211
6d673c20 »
2007-11-26 Made the following renames:
212 def response(&block)
213 # NOTE - we're setting @update for the assert_select_spec - kinda weird, huh?
214 @update = block
1857415f »
2009-03-07 add example specifying template extension (rails-2.3 only for now)
215 super
6d673c20 »
2007-11-26 Made the following renames:
216 end
217
218 def integrate_views!
219 @integrate_views = true
220 end
221
6853e88c »
2009-02-13 general cleanup
222 private
1857415f »
2009-03-07 add example specifying template extension (rails-2.3 only for now)
223
6d673c20 »
2007-11-26 Made the following renames:
224 def integrate_views?
225 @integrate_views
226 end
6bbe80fe »
2008-05-24 Decouple mock framework from global extensions used by rspec.
227
228 def matching_message_expectation_exists(options)
6853e88c »
2009-02-13 general cleanup
229 render_proxy.__send__(:__mock_proxy).__send__(:find_matching_expectation, :render, options)
6bbe80fe »
2008-05-24 Decouple mock framework from global extensions used by rspec.
230 end
231
232 def matching_stub_exists(options)
6853e88c »
2009-02-13 general cleanup
233 render_proxy.__send__(:__mock_proxy).__send__(:find_matching_method_stub, :render, options)
6bbe80fe »
2008-05-24 Decouple mock framework from global extensions used by rspec.
234 end
235
6d673c20 »
2007-11-26 Made the following renames:
236 end
237
238 Spec::Example::ExampleGroupFactory.register(:controller, self)
6853e88c »
2009-02-13 general cleanup
239
e11c5b58 »
2008-08-25 'should render_template' ignores layouts [#508 state:resolved]
240 end
6d673c20 »
2007-11-26 Made the following renames:
241 end
242 end
243 end
Something went wrong with that request. Please try again.