Class: Spec::Rails::Matchers::RenderTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/spec/rails/matchers/render_template.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(expected, controller) ⇒ RenderTemplate

Returns a new instance of RenderTemplate.



7
8
9
10
# File 'lib/spec/rails/matchers/render_template.rb', line 7

def initialize(expected, controller)
  @controller = controller
  @expected = expected
end

Instance Method Details

#descriptionObject



62
63
64
# File 'lib/spec/rails/matchers/render_template.rb', line 62

def description
  "render template #{@expected.inspect}"
end

#failure_message_for_shouldObject



50
51
52
53
54
55
56
# File 'lib/spec/rails/matchers/render_template.rb', line 50

def failure_message_for_should
  if @redirect_url
    "expected #{@expected.inspect}, got redirected to #{@redirect_url.inspect}"
  else
    "expected #{@expected.inspect}, got #{@actual.inspect}"
  end
end

#failure_message_for_should_notObject



58
59
60
# File 'lib/spec/rails/matchers/render_template.rb', line 58

def failure_message_for_should_not
  "expected not to render #{@expected.inspect}, but did"
end

#match_files(actual, expected) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/spec/rails/matchers/render_template.rb', line 41

def match_files(actual, expected)
  actual_parts = actual.split('.')
  expected_parts = expected.split('.')
  expected_parts.each_with_index do |expected_part, index|
    return false unless expected_part == actual_parts[index]
  end
  true
end

#matches?(response_or_controller) ⇒ Boolean

Returns:

  • (Boolean)


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
# File 'lib/spec/rails/matchers/render_template.rb', line 12

def matches?(response_or_controller)
  response  = response_or_controller.respond_to?(:response) ?
              response_or_controller.response :
              response_or_controller

  if response.respond_to?(:redirect?) && response.redirect?
    @redirect_url = response.redirect_url
  elsif response.respond_to?(:rendered_file)
    @actual = response.rendered_file
  elsif response.respond_to?(:rendered)
    case template = response.rendered[:template]
    when nil
      unless response.rendered[:partials].empty?
        @actual = path_and_file(response.rendered[:partials].keys.first).join("/_")
      end
    when ActionView::Template
      @actual = template.path
    when String
      @actual = template
    end
  else
    @actual = response.rendered_template.to_s
  end
  return false if @actual.blank?
  given_controller_path, given_file = path_and_file(@actual)
  expected_controller_path, expected_file = path_and_file(@expected)
  given_controller_path == expected_controller_path && match_files(given_file, expected_file)
end