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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
Feature: View specs
View specs are marked by `type: :view`
or if you have set `config.infer_spec_type_from_file_location!`
by placing them in `spec/views`.
Use them to test the content of view templates
without invoking a specific controller.
They generally follow three steps:
```ruby
assign(:widget, Widget.new) # sets @widget = Widget.new in the view template
render
expect(rendered).to match(/text/)
```
1. Use the `assign` method to set instance variables in the view.
2. Use the `render` method to render the view.
3. Set expectations against the resulting rendered template.
Scenario: View specs render the described view file
Given a file named "spec/views/widgets/index.html.erb_spec.rb" with:
"""ruby
require "rails_helper"
RSpec.describe "widgets/index" , type: :view do
it "displays all the widgets" do
assign(:widgets, [
Widget.create!(:name => "slicer"),
Widget.create!(:name => "dicer")
])
render
expect(rendered).to match /slicer/
expect(rendered).to match /dicer/
end
end
"""
When I run `rspec spec/views`
Then the examples should all pass
Scenario: View specs can have before block and nesting
Given a file named "spec/views/widgets/index.html.erb_spec.rb" with:
"""ruby
require "rails_helper"
RSpec.describe "widgets/index" , type: :view do
context "with 2 widgets" do
before(:each) do
assign(:widgets, [
Widget.create!(:name => "slicer"),
Widget.create!(:name => "dicer")
])
end
it "displays both widgets" do
render
expect(rendered).to match /slicer/
expect(rendered).to match /dicer/
end
end
end
"""
When I run `rspec spec/views`
Then the examples should all pass
Scenario: View specs can explicitly render templates
Given a file named "spec/views/widgets/widget.html.erb_spec.rb" with:
"""ruby
require "rails_helper"
RSpec.describe "rendering the widget template" , type: :view do
it "displays the widget" do
assign(:widget, Widget.create!(:name => "slicer"))
render :template => "widgets/widget"
expect(rendered).to match /slicer/
end
end
"""
And a file named "app/views/widgets/widget.html.erb" with:
"""
<h2><%= @widget.name %></h2>
"""
When I run `rspec spec/views`
Then the examples should all pass
Scenario: View specs can render templates in layouts
Given a file named "spec/views/widgets/widget.html.erb_spec.rb" with:
"""ruby
require "rails_helper"
RSpec.describe "rendering the widget template" , type: :view do
context "with the inventory layout" do
it "displays the widget" do
assign(:widget, Widget.create!(:name => "slicer"))
render :template => "widgets/widget", :layout => "layouts/inventory"
expect(rendered).to match /slicer/
end
end
end
"""
And a file named "app/views/widgets/widget.html.erb" with:
"""
<h2><%= @widget.name %></h2>
"""
And a file named "app/views/layouts/inventory.html.erb" with:
"""
<%= yield %>
"""
When I run `rspec spec/views`
Then the examples should all pass
Scenario: View specs can have description that includes the format and handler
Given a file named "spec/views/widgets/widget.xml.erb_spec.rb" with:
"""ruby
require "rails_helper"
RSpec.describe "widgets/widget.html.erb" , type: :view do
it "renders the HTML template" do
render
expect(rendered).to match /HTML/
end
end
RSpec.describe "widgets/widget.xml.erb" , type: :view do
it "renders the XML template" do
render
expect(rendered).to match /XML/
end
end
"""
And a file named "app/views/widgets/widget.html.erb" with:
"""
HTML
"""
And a file named "app/views/widgets/widget.xml.erb" with:
"""
XML
"""
When I run `rspec spec/views`
Then the examples should all pass
Scenario: View specs can render a template with locals
Given a file named "spec/views/widgets/_widget.html.erb_spec.rb" with:
"""ruby
require "rails_helper"
RSpec.describe "widgets/index" , type: :view do
it "displays the widget" do
widget = Widget.create!(:name => "slicer")
render :locals => {:widget => widget}
expect(rendered).to match /slicer/
end
end
"""
And a file named "app/views/widgets/index.html.erb" with:
"""
<h3><%= widget.name %></h3>
"""
When I run `rspec spec/views`
Then the examples should all pass
Scenario: View specs can render locals in a partial
Given a file named "spec/views/widgets/_widget.html.erb_spec.rb" with:
"""ruby
require "rails_helper"
RSpec.describe "rendering locals in a partial" , type: :view do
it "displays the widget" do
widget = Widget.create!(:name => "slicer")
render :partial => "widgets/widget", :locals => {:widget => widget}
expect(rendered).to match /slicer/
end
end
"""
And a file named "app/views/widgets/_widget.html.erb" with:
"""
<h3><%= widget.name %></h3>
"""
When I run `rspec spec/views`
Then the examples should all pass
Scenario: View specs can render locals in an implicit partial
Given a file named "spec/views/widgets/_widget.html.erb_spec.rb" with:
"""ruby
require "rails_helper"
RSpec.describe "rendering locals in a partial" , type: :view do
it "displays the widget" do
widget = Widget.create!(:name => "slicer")
render "widgets/widget", :widget => widget
expect(rendered).to match /slicer/
end
end
"""
And a file named "app/views/widgets/_widget.html.erb" with:
"""
<h3><%= widget.name %></h3>
"""
When I run `rspec spec/views`
Then the examples should all pass
Scenario: View specs can render text
Given a file named "spec/views/widgets/direct.html.erb_spec.rb" with:
"""ruby
require "rails_helper"
RSpec.describe "rendering text directly" , type: :view do
it "displays the given text" do
render :plain => "This is directly rendered"
expect(rendered).to match /directly rendered/
end
end
"""
When I run `rspec spec/views`
Then the examples should all pass
Scenario: View specs can stub a helper method
Given a file named "app/helpers/application_helper.rb" with:
"""ruby
module ApplicationHelper
def admin?
false
end
end
"""
And a file named "app/views/secrets/index.html.erb" with:
"""
<%- if admin? %>
<h1>Secret admin area</h1>
<%- end %>
"""
And a file named "spec/views/secrets/index.html.erb_spec.rb" with:
"""ruby
require 'rails_helper'
RSpec.describe 'secrets/index' , type: :view do
before do
allow(view).to receive(:admin?).and_return(true)
end
it 'checks for admin access' do
render
expect(rendered).to match /Secret admin area/
end
end
"""
When I run `rspec spec/views/secrets`
Then the examples should all pass
Scenario: View specs use symbols for keys in `request.path_parameters` to match Rails style
Given a file named "spec/views/widgets/index.html.erb_spec.rb" with:
"""ruby
require "rails_helper"
RSpec.describe "controller.request.path_parameters" , type: :view do
it "matches the Rails environment by using symbols for keys" do
[:controller, :action].each { |k| expect(controller.request.path_parameters.keys).to include(k) }
end
end
"""
When I run `rspec spec/views`
Then the examples should all pass
Scenario: View spec actions that do not require extra parameters have `request.fullpath` set
Given a file named "spec/views/widgets/index.html.erb_spec.rb" with:
"""ruby
require "rails_helper"
RSpec.describe "widgets/index" , type: :view do
it "has a request.fullpath that is defined" do
expect(controller.request.fullpath).to eq widgets_path
end
end
"""
When I run `rspec spec/views`
Then the examples should all pass
Scenario: View spec actions that require extra parameters have `request.fullpath` set when the developer supplies them
Given a file named "spec/views/widgets/show.html.erb_spec.rb" with:
"""ruby
require "rails_helper"
RSpec.describe "widgets/show" , type: :view do
it "displays the widget with id: 1" do
widget = Widget.create!(:name => "slicer")
controller.extra_params = { :id => widget.id }
expect(controller.request.fullpath).to eq widget_path(widget)
end
end
"""
When I run `rspec spec/views`
Then the examples should all pass
|