forked from thoughtbot/shoulda-matchers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
render_template_matcher.rb
84 lines (77 loc) · 2.44 KB
/
render_template_matcher.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
module Shoulda
module Matchers
module ActionController
# The `render_template` matcher tests that an action renders a template
# or partial. In RSpec, it is very similar to rspec-rails's
# `render_template` matcher. In a test suite using Minitest + Shoulda, it
# provides a more expressive syntax over `assert_template`.
#
# class PostsController < ApplicationController
# def show
# end
# end
#
# # app/views/posts/show.html.erb
# <%= render 'sidebar' %>
#
# # RSpec
# RSpec.describe PostsController, type: :controller do
# describe 'GET #show' do
# before { get :show }
#
# it { should render_template('show') }
# it { should render_template(partial: '_sidebar') }
# end
# end
#
# # Minitest (Shoulda)
# class PostsControllerTest < ActionController::TestCase
# context 'GET #show' do
# setup { get :show }
#
# should render_template('show')
# should render_template(partial: '_sidebar')
# end
# end
#
# @return [RenderTemplateMatcher]
#
def render_template(options = {}, message = nil)
RenderTemplateMatcher.new(options, message, self)
end
# @private
class RenderTemplateMatcher
attr_reader :failure_message, :failure_message_when_negated
def initialize(options, message, context)
@options = options
@message = message
@template = options.is_a?(Hash) ? options[:partial] : options
@context = context
@controller = nil
@failure_message = nil
@failure_message_when_negated = nil
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?
@context.__send__(:assert_template, @options, @message)
@failure_message_when_negated = "Didn't expect to render #{@template}"
true
rescue Shoulda::Matchers.assertion_exception_class => e
@failure_message = e.message
false
end
end
end
end
end