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