File: template_assertions_test.rb

package info (click to toggle)
ruby-rails-controller-testing 1.0.5-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,016 kB
  • sloc: ruby: 646; makefile: 4
file content (204 lines) | stat: -rw-r--r-- 4,874 bytes parent folder | download
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
require 'test_helper'

class TemplateAssertionsControllerTest < ActionController::TestCase
  def test_with_invalid_hash_keys_raises_argument_error
    assert_raise(ArgumentError) do
      assert_template foo: "bar"
    end
  end

  def test_with_partial
    get :render_with_partial
    assert_template partial: '_partial'
  end

  def test_file_with_relative_path_success
    skip "Deprecated in ActionPack 6.1+" if ActionPack.version >= Gem::Version.new('6.1')

    get :render_file_relative_path
    assert_template file: 'README.rdoc'
  end

  def test_with_file_failure
    get :render_file_absolute_path

    assert_raise(ActiveSupport::TestCase::Assertion) do
      assert_template :file => 'test/hello_world'
    end
  end

  def test_with_nil_passes_when_no_template_rendered
    get :render_nothing
    assert_template nil
  end

  def test_with_nil_fails_when_template_rendered
    get :render_with_template

    assert_raise(ActiveSupport::TestCase::Assertion) do
      assert_template nil
    end
  end

  def test_with_empty_string_fails_when_template_rendered
    get :render_with_template

    assert_raise(ActiveSupport::TestCase::Assertion) do
      assert_template ""
    end
  end

  def test_with_empty_string_fails_when_no_template_rendered
    get :render_nothing

    assert_raise(ActiveSupport::TestCase::Assertion) do
      assert_template ""
    end
  end

  def test_passes_with_correct_string
    get :render_with_template
    assert_template 'hello_world'
    assert_template 'test/hello_world'
  end

  def test_passes_with_correct_symbol
    get :render_with_template
    assert_template :hello_world
  end

  def test_fails_with_incorrect_string
    get :render_with_template

    assert_raise(ActiveSupport::TestCase::Assertion) do
      assert_template 'hello_planet'
    end
  end

  def test_fails_with_incorrect_string_that_matches
    get :render_with_template

    assert_raise(ActiveSupport::TestCase::Assertion) do
      assert_template 'est/he'
    end
  end

  def test_fails_with_repeated_name_in_path
    get :render_with_template_repeating_in_path

    assert_raise(ActiveSupport::TestCase::Assertion) do
      assert_template 'test/hello'
    end
  end

  def test_fails_with_incorrect_symbol
    get :render_with_template

    assert_raise(ActiveSupport::TestCase::Assertion) do
      assert_template :hello_planet
    end
  end

  def test_fails_with_incorrect_symbol_that_matches
    get :render_with_template

    assert_raise(ActiveSupport::TestCase::Assertion) do
      assert_template :"est/he"
    end
  end

  def test_fails_with_wrong_layout
    get :render_with_layout

    assert_raise(ActiveSupport::TestCase::Assertion) do
      assert_template layout: "application"
    end
  end

  def test_fails_expecting_no_layout
    get :render_with_layout

    assert_raise(ActiveSupport::TestCase::Assertion) do
      assert_template layout: nil
    end
  end

  def test_fails_expecting_not_known_layout
    get :render_with_layout

    assert_raise(ArgumentError) do
      assert_template layout: 1
    end
  end

  def test_passes_with_correct_layout
    get :render_with_layout
    assert_template layout: "layouts/standard"
  end

  def test_passes_with_layout_and_partial
    get :render_with_layout_and_partial
    assert_template layout: "layouts/standard"
    assert_template partial: "test/_partial"
  end

  def test_passed_with_no_layout
    get :render_with_template
    assert_template layout: nil
  end

  def test_passed_with_no_layout_false
    get :render_with_template
    assert_template layout: false
  end

  def test_passes_with_correct_layout_without_layouts_prefix
    get :render_with_layout
    assert_template layout: "standard"
  end

  def test_passes_with_correct_layout_symbol
    get :render_with_layout
    assert_template layout: :standard
  end

  def test_assert_template_reset_between_requests
    get :render_with_template
    assert_template 'test/hello_world'

    get :render_nothing
    assert_template nil

    get :render_with_partial
    assert_template partial: 'test/_partial'

    get :render_nothing
    assert_template partial: nil

    get :render_with_layout
    assert_template layout: 'layouts/standard'

    get :render_nothing
    assert_template layout: nil

    if ActionPack.version < Gem::Version.new('6.1')
      get :render_file_relative_path
      assert_template file: 'README.rdoc'
    end

    get :render_nothing
    assert_template file: nil
  end

  def test_locals_option_to_assert_template_is_not_supported
    get :render_nothing

    warning_buffer = StringIO.new
    $stderr = warning_buffer

    assert_template partial: 'customer_greeting', locals: { greeting: 'Bonjour' }
    assert_includes warning_buffer.string, "the :locals option to #assert_template is only supported in a ActionView::TestCase\n"
  ensure
    $stderr = STDERR
  end
end