2010-12-15 17:34:19 -05:00
|
|
|
module Shoulda # :nodoc:
|
|
|
|
module Matchers
|
|
|
|
module ActionController # :nodoc:
|
|
|
|
# Ensures a controller rendered the given template.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# it { should render_template(:show) }
|
2011-10-15 07:33:51 -04:00
|
|
|
#
|
|
|
|
# assert that the "_customer" partial was rendered
|
2014-01-17 15:20:44 -05:00
|
|
|
# it { should render_template(partial: '_customer') }
|
2011-10-15 07:33:51 -04:00
|
|
|
#
|
|
|
|
# assert that the "_customer" partial was rendered twice
|
2014-01-17 15:20:44 -05:00
|
|
|
# it { should render_template(partial: '_customer', count: 2) }
|
2011-10-15 07:33:51 -04:00
|
|
|
#
|
|
|
|
# assert that no partials were rendered
|
2014-01-17 15:20:44 -05:00
|
|
|
# it { should render_template(partial: false) }
|
2011-10-15 07:33:51 -04:00
|
|
|
def render_template(options = {}, message = nil)
|
|
|
|
RenderTemplateMatcher.new(options, message, self)
|
2010-12-15 17:34:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class RenderTemplateMatcher # :nodoc:
|
2013-12-24 06:24:27 -05:00
|
|
|
attr_reader :failure_message, :failure_message_when_negated
|
|
|
|
|
|
|
|
alias failure_message_for_should failure_message
|
|
|
|
alias failure_message_for_should_not failure_message_when_negated
|
2010-12-15 17:34:19 -05:00
|
|
|
|
2011-10-15 07:33:51 -04:00
|
|
|
def initialize(options, message, context)
|
|
|
|
@options = options
|
|
|
|
@message = message
|
|
|
|
@template = options.is_a?(Hash) ? options[:partial] : options
|
2010-12-15 17:34:19 -05:00
|
|
|
@context = context
|
|
|
|
end
|
|
|
|
|
|
|
|
def matches?(controller)
|
|
|
|
@controller = controller
|
|
|
|
renders_template?
|
|
|
|
end
|
|
|
|
|
|
|
|
def description
|
|
|
|
"render template #{@template}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def in_context(context)
|
|
|
|
@context = context
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def renders_template?
|
|
|
|
begin
|
2011-10-15 07:33:51 -04:00
|
|
|
@context.send(:assert_template, @options, @message)
|
2013-12-24 06:24:27 -05:00
|
|
|
@failure_message_when_negated = "Didn't expect to render #{@template}"
|
2010-12-15 17:34:19 -05:00
|
|
|
true
|
2011-02-03 12:13:11 -05:00
|
|
|
rescue Shoulda::Matchers::AssertionError => error
|
2013-12-24 06:24:27 -05:00
|
|
|
@failure_message = error.message
|
2010-12-15 17:34:19 -05:00
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|