Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 186 lines (152 sloc) 5.215 kb
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
Feature: view spec

View specs live in spec/views and render view templates in isolation.

Scenario: passing spec that renders the described view file
Given a file named "spec/views/widgets/index.html.erb_spec.rb" with:
"""
require "spec_helper"

describe "widgets/index.html.erb" do
it "displays all the widgets" do
assign(:widgets, [
stub_model(Widget, :name => "slicer"),
stub_model(Widget, :name => "dicer")
])

render

rendered.should contain("slicer")
rendered.should contain("dicer")
end
end
"""
When I run "rspec spec/views"
Then the examples should all pass

Scenario: passing spec with before and nesting
Given a file named "spec/views/widgets/index.html.erb_spec.rb" with:
"""
require "spec_helper"

describe "widgets/index.html.erb" do

context "with 2 widgets" do
before(:each) do
assign(:widgets, [
stub_model(Widget, :name => "slicer"),
stub_model(Widget, :name => "dicer")
])
end

it "displays both widgets" do
render

rendered.should contain("slicer")
rendered.should contain("dicer")
end
end
end
"""
When I run "rspec spec/views"
Then the examples should all pass

Scenario: passing spec with explicit template rendering
Given a file named "spec/views/widgets/widget.html.erb_spec.rb" with:
"""
require "spec_helper"

describe "rendering the widget template" do
it "displays the widget" do
assign(:widget, stub_model(Widget, :name => "slicer"))

render :template => "widgets/widget.html.erb"

rendered.should contain("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: passing spec with rendering of locals in a partial
Given a file named "spec/views/widgets/_widget.html.erb_spec.rb" with:
"""
require "spec_helper"

describe "rendering locals in a partial" do
it "displays the widget" do
widget = stub_model(Widget, :name => "slicer")

render :partial => "widgets/widget.html.erb", :locals => {:widget => widget}

rendered.should contain("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: passing spec with rendering of locals in an implicit partial
Given a file named "spec/views/widgets/_widget.html.erb_spec.rb" with:
"""
require "spec_helper"

describe "rendering locals in a partial" do
it "displays the widget" do
widget = stub_model(Widget, :name => "slicer")

render "widgets/widget", :widget => widget

rendered.should contain("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: passing spec with rendering of text
Given a file named "spec/views/widgets/direct.html.erb_spec.rb" with:
"""
require "spec_helper"

describe "rendering text directly" do
it "displays the given text" do

render :text => "This is directly rendered"

rendered.should contain("directly rendered")
end
end
"""
When I run "rspec spec/views"
Then the examples should all pass

Scenario: passing spec with rendering of Prototype helper update
Given a file named "spec/views/widgets/prototype_update.html.erb_spec.rb" with:
"""
require "spec_helper"

describe "render a Prototype helper update" do
it "hides the status indicator" do

render :update do |page|
page.hide 'status-indicator'
end

rendered.should contain("Element.hide(\"status-indicator\")")
end
end
"""
When I run "rspec spec/views"
Then the examples should all pass

Scenario: spec with view that accesses helper_method helpers
Given 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:
"""
require 'spec_helper'

describe 'secrets/index.html.erb' do
before do
controller.singleton_class.class_eval do
protected
def admin?
true
end
helper_method :admin?
end
end

it 'checks for admin access' do
render
rendered.should contain('Secret admin area')
end
end
"""
When I run "rspec spec/views/secrets"
Then the examples should all pass
Something went wrong with that request. Please try again.