Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

record render instead of rendering if the template exists

[#902 state:resolved milestone:'Next Release']
  • Loading branch information...
commit 85e264e2049accf4e5fd3b22cf38eca2960467c2 1 parent ca60541
David Chelimsky authored
59 lib/spec/rails/example/controller_example_group.rb
View
@@ -43,7 +43,7 @@ module Example
#
class ControllerExampleGroup < FunctionalExampleGroup
class << self
-
+
# Use integrate_views to instruct RSpec to render views in
# your controller examples in Integration mode.
#
@@ -56,11 +56,11 @@ class << self
def integrate_views(integrate_views = true)
@integrate_views = integrate_views
end
-
+
def integrate_views? # :nodoc:
@integrate_views
end
-
+
def inherited(klass) # :nodoc:
klass.integrate_views(integrate_views?)
klass.subject { controller }
@@ -93,7 +93,7 @@ def controller_name(name)
tests "#{name}_controller".camelize.constantize
end
end
-
+
before(:each) do
# Some Rails apps explicitly disable ActionMailer in environment.rb
if defined?(ActionMailer)
@@ -107,7 +107,7 @@ def controller_name(name)
indicate this by passing the controller to describe():
describe MyController do
-
+
or by declaring the controller's name
describe "a MyController" do
@@ -121,7 +121,7 @@ def controller_name(name)
end
attr_reader :response, :request, :controller
-
+
def integrate_views?
@integrate_views || self.class.integrate_views?
end
@@ -143,7 +143,7 @@ def controller.rescue_action_with_handler(exception)
end
end
end
-
+
protected
def _assigns_hash_proxy
@@ -151,27 +151,33 @@ def _assigns_hash_proxy
end
private
-
+
module TemplateIsolationExtensions
def file_exists?(ignore); true; end
-
+
def render_file(*args)
@first_render ||= args[0] unless args[0] =~ /^layouts/
end
-
+
# Rails 2.2
def _pick_template(*args)
@_first_render ||= args[0] unless args[0] =~ /^layouts/
PickedTemplate.new
end
-
+
def __action_exists?(params)
controller.respond_to? params[:action]
end
-
+
+ def __template_exists?(args)
+ self.view_paths.respond_to?(:find_template) ?
+ self.view_paths.find_template(args[0][:file], template_format) :
+ false
+ end
+
def render(*args)
if ::Rails::VERSION::STRING >= "2.1"
- return super unless __action_exists?(params)
+ return super unless __action_exists?(params) || __template_exists?(args)
end
if file = args.last[:file].instance_eval{@template_path}
record_render :file => file
@@ -183,23 +189,24 @@ def render(*args)
super
end
end
-
+
private
-
+
def record_render(opts)
- (@_rendered[:template] ||= opts[:file]) if opts[:file]
- (@_rendered[:partials][opts[:partial]] += 1) if opts[:partial]
+ return unless @_rendered
+ @_rendered[:template] ||= opts[:file] if opts[:file]
+ @_rendered[:partials][opts[:partial]] += 1 if opts[:partial]
end
-
+
# Returned by _pick_template when running controller examples in isolation mode.
- class PickedTemplate
+ class PickedTemplate
# Do nothing when running controller examples in isolation mode.
def render_template(*ignore_args); end
# Do nothing when running controller examples in isolation mode.
def render_partial(*ignore_args); end
end
end
-
+
module ControllerInstanceMethods # :nodoc:
include Spec::Rails::Example::RenderObserver
@@ -230,7 +237,7 @@ def render(options=nil, extra_options={}, &block)
end
end
end
-
+
# Rails 2.3
def default_template(action_name = self.action_name)
if integrate_views?
@@ -243,7 +250,7 @@ def default_template(action_name = self.action_name)
end
end
end
-
+
def response(&block)
# NOTE - we're setting @update for the assert_select_spec - kinda weird, huh?
@update = block
@@ -255,7 +262,7 @@ def integrate_views!
end
private
-
+
def integrate_views?
@integrate_views
end
@@ -263,15 +270,15 @@ def integrate_views?
def matching_message_expectation_exists(options)
render_proxy.__send__(:__mock_proxy).__send__(:find_matching_expectation, :render, options)
end
-
+
def matching_stub_exists(options)
render_proxy.__send__(:__mock_proxy).__send__(:find_matching_method_stub, :render, options)
end
-
+
end
Spec::Example::ExampleGroupFactory.register(:controller, self)
-
+
end
end
end
1  spec/resources/views/controller_spec/non_existent_action_with_existent_template.html.erb
View
@@ -0,0 +1 @@
+<%- raise "THIS TEMPLATE SHOULD NOT BE RENDERED" -%>
Please sign in to comment.
Something went wrong with that request. Please try again.