changelog shortlog tags changeset manifest revisions annotate raw

vendor/plugins/rspec_on_rails/spec/rails/example/view_spec_spec.rb

changeset 16: 01fd3f10ae84
author: moriq@moriq.com
date: Mon Mar 10 10:13:18 2008 +0900 (16 years ago)
permissions: -rw-r--r--
description: add plugins rspec_on_rails
1require File.dirname(__FILE__) + '/../../spec_helper'
2
3describe "A template with an implicit helper", :type => :view do
4 before(:each) do
5 render "view_spec/implicit_helper"
6 end
7
8 it "should include the helper" do
9 response.should have_tag('div', :content => "This is text from a method in the ViewSpecHelper")
10 end
11
12 it "should include the application helper" do
13 response.should have_tag('div', :content => "This is text from a method in the ApplicationHelper")
14 end
15
16 it "should have access to named routes" do
17 rspec_on_rails_specs_url.should == "http://test.host/rspec_on_rails_specs"
18 rspec_on_rails_specs_path.should == "/rspec_on_rails_specs"
19 end
20end
21
22describe "A template requiring an explicit helper", :type => :view do
23 before(:each) do
24 render "view_spec/explicit_helper", :helper => 'explicit'
25 end
26
27 it "should include the helper if specified" do
28 response.should have_tag('div', :content => "This is text from a method in the ExplicitHelper")
29 end
30
31 it "should include the application helper" do
32 response.should have_tag('div', :content => "This is text from a method in the ApplicationHelper")
33 end
34end
35
36describe "A template requiring multiple explicit helpers", :type => :view do
37 before(:each) do
38 render "view_spec/multiple_helpers", :helpers => ['explicit', 'more_explicit']
39 end
40
41 it "should include all specified helpers" do
42 response.should have_tag('div', :content => "This is text from a method in the ExplicitHelper")
43 response.should have_tag('div', :content => "This is text from a method in the MoreExplicitHelper")
44 end
45
46 it "should include the application helper" do
47 response.should have_tag('div', :content => "This is text from a method in the ApplicationHelper")
48 end
49end
50
51describe "Message Expectations on helper methods", :type => :view do
52 it "should work" do
53 template.should_receive(:method_in_plugin_application_helper).and_return('alternate message 1')
54 render "view_spec/implicit_helper"
55 response.body.should =~ /alternate message 1/
56 end
57
58 it "should work twice" do
59 template.should_receive(:method_in_plugin_application_helper).and_return('alternate message 2')
60 render "view_spec/implicit_helper"
61 response.body.should =~ /alternate message 2/
62 end
63end
64
65describe "A template that includes a partial", :type => :view do
66 def render!
67 render "view_spec/template_with_partial"
68 end
69
70 it "should render the enclosing template" do
71 render!
72 response.should have_tag('div', "method_in_partial in ViewSpecHelper")
73 end
74
75 it "should render the partial" do
76 render!
77 response.should have_tag('div', "method_in_template_with_partial in ViewSpecHelper")
78 end
79
80 it "should include the application helper" do
81 render!
82 response.should have_tag('div', "This is text from a method in the ApplicationHelper")
83 end
84
85 it "should pass expect_render with the right partial" do
86 template.expect_render(:partial => 'partial')
87 render!
88 template.verify_rendered
89 end
90
91 it "should fail expect_render with the wrong partial" do
92 template.expect_render(:partial => 'non_existent')
93 render!
94 begin
95 template.verify_rendered
96 rescue Spec::Mocks::MockExpectationError => e
97 ensure
98 e.backtrace.find{|line| line =~ /view_spec_spec\.rb\:92/}.should_not be_nil
99 end
100 end
101
102 it "should pass expect_render when a partial is expected twice and happens twice" do
103 template.expect_render(:partial => 'partial_used_twice').twice
104 render!
105 template.verify_rendered
106 end
107
108 it "should pass expect_render when a partial is expected once and happens twice" do
109 template.expect_render(:partial => 'partial_used_twice')
110 render!
111 begin
112 template.verify_rendered
113 rescue Spec::Mocks::MockExpectationError => e
114 ensure
115 e.backtrace.find{|line| line =~ /view_spec_spec\.rb\:109/}.should_not be_nil
116 end
117 end
118
119 it "should fail expect_render with the right partial but wrong options" do
120 template.expect_render(:partial => 'partial', :locals => {:thing => Object.new})
121 render!
122 lambda {template.verify_rendered}.should raise_error(Spec::Mocks::MockExpectationError)
123 end
124end
125
126describe "A partial that includes a partial", :type => :view do
127 it "should support expect_render with nested partial" do
128 obj = Object.new
129 template.expect_render(:partial => 'partial', :object => obj)
130 render :partial => "view_spec/partial_with_sub_partial", :locals => { :partial => obj }
131 end
132end
133
134describe "A view that includes a partial using :collection and :spacer_template", :type => :view do
135 it "should render the partial w/ spacer_tamplate" do
136 render "view_spec/template_with_partial_using_collection"
137 response.should have_tag('div',/method_in_partial/)
138 response.should have_tag('div',/ApplicationHelper/)
139 response.should have_tag('div',/ViewSpecHelper/)
140 response.should have_tag('hr#spacer')
141 end
142
143 it "should render the partial" do
144 template.expect_render(:partial => 'partial',
145 :collection => ['Alice', 'Bob'],
146 :spacer_template => 'spacer')
147 render "view_spec/template_with_partial_using_collection"
148 end
149
150end
151
152describe "A view that includes a partial using an array as partial_path", :type => :view do
153 before(:each) do
154 module ActionView::Partials
155 def render_template_with_partial_with_array_support(partial_path, local_assigns = nil, deprecated_local_assigns = nil)
156 if partial_path.is_a?(Array)
157 "Array Partial"
158 else
159 render_partial_without_array_support(partial_path, local_assigns, deprecated_local_assigns)
160 end
161 end
162
163 alias :render_partial_without_array_support :render_partial
164 alias :render_partial :render_template_with_partial_with_array_support
165 end
166
167 @array = ['Alice', 'Bob']
168 assigns[:array] = @array
169 end
170
171 after(:each) do
172 module ActionView::Partials
173 alias :render_template_with_partial_with_array_support :render_partial
174 alias :render_partial :render_partial_without_array_support
175 undef render_template_with_partial_with_array_support
176 end
177 end
178
179 it "should render have the array passed through to render_partial without modification" do
180 render "view_spec/template_with_partial_with_array"
181 response.body.should match(/^Array Partial$/)
182 end
183end
184
185describe "Different types of renders (not :template)", :type => :view do
186 it "should render partial with local" do
187 render :partial => "view_spec/partial_with_local_variable", :locals => {:x => "Ender"}
188 response.should have_tag('div', :content => "Ender")
189 end
190end
191
192describe "A view", :type => :view do
193 before(:each) do
194 session[:key] = "session"
195 params[:key] = "params"
196 flash[:key] = "flash"
197 render "view_spec/accessor"
198 end
199
200 it "should have access to session data" do
201 response.should have_tag("div#session", "session")
202 end
203
204 specify "should have access to params data" do
205 response.should have_tag("div#params", "params")
206 end
207
208 it "should have access to flash data" do
209 response.should have_tag("div#flash", "flash")
210 end
211end
212
213describe "A view with a form_tag", :type => :view do
214 it "should render the right action" do
215 render "view_spec/entry_form"
216 response.should have_tag("form[action=?]","/view_spec/entry_form")
217 end
218end
219
220describe "An instantiated ViewExampleGroupController", :type => :view do
221 before do
222 render "view_spec/foo/show"
223 end
224
225 it "should return the name of the real controller that it replaces" do
226 @controller.controller_name.should == 'foo'
227 end
228
229 it "should return the path of the real controller that it replaces" do
230 @controller.controller_path.should == 'view_spec/foo'
231 end
232end
233
234module Spec
235 module Rails
236 module Example
237 describe ViewExampleGroup do
238 it "should clear its name from the description" do
239 group = describe("foo", :type => :view) do
240 $nested_group = describe("bar") do
241 end
242 end
243 group.description.to_s.should == "foo"
244 $nested_group.description.to_s.should == "foo bar"
245 end
246
247 it "should clear ActionView::Base.base_view_path on teardown" do
248 ViewExampleGroup.class_eval do
249 alias_method(:ensure_that_base_view_path_is_not_set_across_example_groups_orig,
250 :ensure_that_base_view_path_is_not_set_across_example_groups)
251 define_method(:ensure_that_base_view_path_is_not_set_across_example_groups){
252 $base_view_path_cleared = true
253 ensure_that_base_view_path_is_not_set_across_example_groups_orig
254 }
255 end
256 describe("base_view_path_cleared flag", :type => :view) do
257 it { $base_view_path_cleared.should be_true }
258 end
259 end
260 end
261 end
262 end
263end