1
0
Fork 0
mirror of https://github.com/thoughtbot/shoulda-matchers.git synced 2022-11-09 12:01:38 -05:00
thoughtbot--shoulda-matchers/lib/shoulda/matchers/action_controller/render_template_matcher.rb

65 lines
1.8 KiB
Ruby
Raw Normal View History

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) }
#
# assert that the "_customer" partial was rendered
2014-01-17 15:20:44 -05:00
# it { should render_template(partial: '_customer') }
#
# assert that the "_customer" partial was rendered twice
2014-01-17 15:20:44 -05:00
# it { should render_template(partial: '_customer', count: 2) }
#
# assert that no partials were rendered
2014-01-17 15:20:44 -05:00
# it { should render_template(partial: false) }
def render_template(options = {}, message = nil)
RenderTemplateMatcher.new(options, message, self)
2010-12-15 17:34:19 -05:00
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
2010-12-15 17:34:19 -05: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
@context.send(:assert_template, @options, @message)
@failure_message_when_negated = "Didn't expect to render #{@template}"
2010-12-15 17:34:19 -05:00
true
rescue Shoulda::Matchers::AssertionError => error
@failure_message = error.message
2010-12-15 17:34:19 -05:00
false
end
end
end
end
end
end