<br><br><div><span class="gmail_quote">On 7/6/07, <b class="gmail_sendername">David Chelimsky</b> &lt;<a href="mailto:dchelimsky@gmail.com">dchelimsky@gmail.com</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On 7/6/07, Daniel N &lt;<a href="mailto:has.sox@gmail.com">has.sox@gmail.com</a>&gt; wrote:<br>&gt; On 7/6/07, Daniel N &lt;<a href="mailto:has.sox@gmail.com">has.sox@gmail.com</a>&gt; wrote:<br>&gt; &gt; On 7/6/07, David Chelimsky &lt; 
<a href="mailto:dchelimsky@gmail.com">dchelimsky@gmail.com</a>&gt; wrote:<br>&gt; &gt; &gt; On 7/5/07, Daniel N &lt;<a href="mailto:has.sox@gmail.com">has.sox@gmail.com</a>&gt; wrote:<br>&gt; &gt; &gt; &gt; Hi,<br>&gt; &gt; &gt; &gt;
<br>&gt; &gt; &gt; &gt; I&#39;m very new to rspec so please be patient with me.<br>&gt; &gt; &gt; &gt;<br>&gt; &gt; &gt; &gt; I&#39;ve tried to take some of my tests out of the controller specs to<br>&gt; check for<br>&gt; &gt; &gt; &gt; things that are rendered.
<br>&gt; &gt; &gt; &gt;<br>&gt; &gt; &gt; &gt; This has not worked so well, since my views have the controller method<br>&gt; &gt; &gt; &gt;<br>&gt; &gt; &gt; &gt; current_user<br>&gt; &gt; &gt; &gt;<br>&gt; &gt; &gt; &gt; in quite a few places.
<br>&gt; &gt; &gt; &gt;<br>&gt; &gt; &gt; &gt; Is there any way that I can define this so that my views will be<br>&gt; executed?<br>&gt; &gt; &gt; &gt; Will this same thing occur for all helper methods that are definied in
<br>&gt; the<br>&gt; &gt; &gt; &gt; controller?<br>&gt; &gt; &gt;<br>&gt; &gt; &gt; If I understand you correctly, you are trying to take tests for views<br>&gt; &gt; &gt; that were previously rails functional tests and turn them into rspec
<br>&gt; &gt; &gt; view examples. If that is the case, you should be able to do this:<br>&gt; &gt; &gt;<br>&gt; &gt; &gt;<br>&gt; template.stub!(:current_user).and_return(mock_model(User))<br>&gt; &gt; &gt;<br>&gt; &gt; &gt; If not, please provide an explicit example so we can better understand
<br>&gt; &gt; &gt; what you&#39;re talking about.<br>&gt; &gt; &gt;<br>&gt; &gt; &gt; Cheers,<br>&gt; &gt; &gt; David<br>&gt; &gt;<br>&gt; &gt;<br>&gt; &gt;<br>&gt; &gt; Thanx David,<br>&gt; &gt;<br>&gt; &gt; That looks like what I was looking for.
<br>&gt; &gt;<br>&gt; &gt; I will try it when I get home.<br>&gt; &gt;<br>&gt; &gt; Cheers<br>&gt; &gt; Daniel<br>&gt;<br>&gt; Ok I&#39;ve started to have a crack at this but it&#39;s getting way out of hand.<br>&gt; Everytime I stub a method there&#39;s another one to do.
<br>&gt;<br>&gt; I&#39;ve also found that when there&#39;s a partial _detail and I&#39;ve passed the<br>&gt; :collection =&gt; @things to it it blows up complaining that the local variable<br>&gt; is nil in<br>&gt; dom_id( detail )
<br><br>If you&#39;re using the trunk, you can do this:<br><br>template.expects_render(:partial =&gt; &#39;detail&#39;, :collection =&gt; @things)<br><br>&gt;<br>&gt; Am I doing someting wrong?&nbsp;&nbsp;The start of my specs is<br>
&gt;<br>&gt;&nbsp;&nbsp; before do<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; @u = mock_model( User )<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; @book = mock_model( Book )<br>&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; public_book = mock_model( Book )<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; private_book = mock_model( Book )<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; public_book.stub!(:title=).and_return( &quot;Public Title&quot; )
<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; private_book.stub!(:title=).and_return( &quot;Private Title&quot;<br>&gt; )<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; public_book.stub!( :title ).and_return( &quot;Public Title&quot; )<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; private_book.stub!( :title ).and_return( &quot;Private Title&quot; )
<br><br>Why are you stubbing the same calls twice each?<br><br>&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; @u.stub!( :public_books ).and_return( [public_book] )<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; @u.stub!( :private_books ).and_return( [private_book] )<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; @clip = mock_model( Clip )
<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; @clip.stub!( :id ).and_return( 1 )<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; @clips = [ @clip ]<br>&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; template.stub!( :current_user ).and_return( @u )<br>&gt;&nbsp;&nbsp; end<br>&gt;<br>&gt; and I&#39;ve only started.&nbsp;&nbsp;Is it normal to have to stub this much for a view?
<br><br>Depends on how much stuff is in your view :)<br><br>You&#39;ve got a couple of options. You could create instances of the<br>model instead. As long as you&#39;re not saving and retrieving them<br>there&#39;s very little db interaction - just enough for AR to discover
<br>the attributes.<br><br>If you prefer to keep it all mocked/stubbed, you could clean up a bit like this:<br><br>before do<br>&nbsp;&nbsp;@u = mock_model( User )<br>&nbsp;&nbsp;@book = mock_model( Book )<br><br>&nbsp;&nbsp;public_book = mock_model(Book, :title =&gt; &#39;Public Title&#39;)
<br>&nbsp;&nbsp;private_book = mock_model( Book, :title =&gt; &#39;Private Title&#39;)<br><br>&nbsp;&nbsp;@u.stub!( :public_books ).and_return( [public_book] )<br>&nbsp;&nbsp;@u.stub!( :private_books ).and_return( [private_book] )<br><br>&nbsp;&nbsp;@clips = [ @clip = mock_model( Clip, :id =&gt; 1 ) ]
<br><br>&nbsp;&nbsp;template.stub!( :current_user ).and_return( @u )<br>end<br><br>That stubs the same amount of stuff, but its a little cleaner. You<br>could also write a shared behaviour that stubs current user:<br><br>describe &quot;authenticated page view&quot;, :shared =&gt; true do
<br>&nbsp;&nbsp;before(:each) do<br>&nbsp;&nbsp;&nbsp;&nbsp;template.stub!( :current_user ).and_return( @u )<br>&nbsp;&nbsp;end<br>end<br><br>describe &quot;/some/page&quot;<br>&nbsp;&nbsp;it_should_behave_like &quot;authenticated page view&quot;<br><br>&nbsp;&nbsp;before(:each) do
<br>&nbsp;&nbsp;&nbsp;&nbsp;@u = mock_model( User )<br>&nbsp;&nbsp;&nbsp;&nbsp;@book = mock_model( Book )<br><br>&nbsp;&nbsp;&nbsp;&nbsp;public_book = mock_model(Book, :title =&gt; &#39;Public Title&#39;)<br>&nbsp;&nbsp;&nbsp;&nbsp;private_book = mock_model( Book, :title =&gt; &#39;Private Title&#39;)
<br><br>&nbsp;&nbsp;&nbsp;&nbsp;@u.stub!( :public_books ).and_return( [public_book] )<br>&nbsp;&nbsp;&nbsp;&nbsp;@u.stub!( :private_books ).and_return( [private_book] )<br><br>&nbsp;&nbsp;&nbsp;&nbsp;@clips = [ @clip = mock_model( Clip, :id =&gt; 1 ) ]<br><br>&nbsp;&nbsp;end<br>end<br><br>There&#39;s probably a bit more to clean up but I&#39;d have to see the view
<br>code. Would you mind letting us see it?<br><br>&gt;<br>&gt; Cheers<br>&gt; Daniel</blockquote><div><br><br>index.html.erb<br><br>&lt;% content_for :action_bar do %&gt;<br>&nbsp; &lt;% @public_books = current_user.public_books %&gt;
<br>&nbsp;&nbsp; &nbsp;&lt;% @private_books = current_user.private_books %&gt;<br>&nbsp; &lt;%= render :file =&gt; &#39;books/_book_list.html.erb&#39; -%&gt;<br>&lt;% end %&gt;<br><br>&lt;% if @book %&gt;<br>&nbsp; &lt;div class=&#39;book_clip_list&#39; id=&#39;&lt;%= dom_id( @book )-%&gt;&#39;&gt;
<br>&nbsp;&nbsp; &nbsp;&nbsp; &lt;h2 class=&#39;book_header&#39;&gt;&lt;%= @book.title -%&gt;&lt;/h2&gt;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;ul id=&quot;book_clip_menu&quot;&gt;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;li&gt;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;button class=&#39;collapse_all&#39;&gt;Collapse All&lt;/button&gt;
<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;button class=&#39;expand_all&#39;&gt;Expand All&lt;/button&gt;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;/li&gt;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;/ul&gt;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;%= render :partial =&gt; &#39;detail&#39;, :collection =&gt; @clips %&gt;
<br>&nbsp;&nbsp; &nbsp;&lt;/div&gt;<br>&lt;% else %&gt;<br>&nbsp; &lt;%= render :partial =&gt; &#39;detail&#39;, :collection =&gt; @clips -%&gt;<br><br>&lt;% end %&gt;<br><br></div><br></div>detail.html.erb<br>&lt;div id=&#39;&lt;%= dom_id( detail ) -%&gt;&#39; class=&#39;clip_container&#39;&gt;
<br>&nbsp;&nbsp;&nbsp; &lt;div class=&#39;clip_header&#39;&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;button class=&#39;minimize&#39;&gt;-&lt;/button&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;h3 id=&#39;&lt;%= &quot;#{dom_id( detail )}_header&quot; -%&gt;&#39;&gt;&lt;%= detail.title
 -%&gt;&lt;/h3&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;div&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;a href=&#39;&lt;%= detail.url -%&gt;&#39; target=&#39;_blank&#39;&gt;&lt;%= URI.parse(detail.url).host -%&gt;&lt;/a&gt;&lt;br/&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;%= detail.created_at.to_s
( :long ) -%&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/div&gt;<br>&nbsp;&nbsp;&nbsp; &lt;/div&gt;<br>&nbsp;&nbsp;&nbsp; &lt;div class=&#39;content_container&#39; id=&#39;&lt;%= &quot;#{dom_id( detail )}_content_container&quot; -%&gt;&#39;&gt;<br>&nbsp;&nbsp;&nbsp; &lt;div class=&#39;clip_menu&#39;&gt;
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;ul class=&#39;action_menu&#39;&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;%= render :partial =&gt; &#39;action_menu_links&#39;, :locals =&gt; { :clip =&gt; detail, :book =&gt; @book } -%&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/ul&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;ul class=&#39;view_menu&#39;&gt;
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;%= render :partial =&gt; &#39;alternative_content_links&#39;, :locals =&gt; { :clip =&gt; detail } -%&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/ul&gt;<br>&nbsp;&nbsp;&nbsp; &lt;/div&gt;<br>&nbsp;&nbsp;&nbsp; &lt;blockquote id=&#39;&lt;%= &quot;#{dom_id( detail )}_blockquote&quot;-%&gt;&#39;&gt;
<br>&nbsp;&nbsp;&nbsp; &lt;% render_as = @view_type || &#39;html&#39; %&gt;<br>&nbsp;&nbsp;&nbsp; &lt;% render_as = &#39;html&#39; unless Clip.available_types.include?( render_as ) %&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;% case render_as %&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;% when &#39;quote&#39; %&gt;
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;%= render :partial =&gt; &#39;text&#39;, &nbsp;&nbsp;&nbsp; :locals =&gt; { :clip =&gt; detail } %&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;% when &#39;images&#39; %&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &lt;%= render :partial =&gt; &#39;images&#39;, &nbsp;&nbsp;&nbsp; :locals =&gt; { :clip =&gt; detail } %&gt;
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;% when &#39;html&#39; %&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;%= render :partial =&gt; &#39;html&#39;,&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; :locals =&gt; { :clip =&gt; detail } %&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;% end %&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; &lt;/blockquote&gt;<br>
&nbsp;&nbsp;&nbsp; &lt;/div&gt;<br>&lt;/div&gt;<br><br><br>The _text, _images, _html partials are very simple so I won&#39;t show them here.&nbsp; This will be enough I think to evaluate it.<br><br>Thankyou for helping with this.<br><br>Daniel
<br>