thoughtbot--shoulda-matchers/lib/shoulda/matchers/action_controller/render_template_matcher.rb

65 lines
1.8 KiB
Ruby

module Shoulda # :nodoc:
module Matchers
module ActionController # :nodoc:
# Ensures a controller rendered the given template.
#
# Example:
#
# it { should render_template(:show) }
#
# assert that the "_customer" partial was rendered
# it { should render_template(partial: '_customer') }
#
# assert that the "_customer" partial was rendered twice
# it { should render_template(partial: '_customer', count: 2) }
#
# assert that no partials were rendered
# it { should render_template(partial: false) }
def render_template(options = {}, message = nil)
RenderTemplateMatcher.new(options, message, self)
end
class RenderTemplateMatcher # :nodoc:
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
def initialize(options, message, context)
@options = options
@message = message
@template = options.is_a?(Hash) ? options[:partial] : options
@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
@context.__send__(:assert_template, @options, @message)
@failure_message_when_negated = "Didn't expect to render #{@template}"
true
rescue Shoulda::Matchers::AssertionError => error
@failure_message = error.message
false
end
end
end
end
end
end