Well, I'm going to break the rules and post a reply to my own message - 
mostly because nobody else has replied! Which makes me think that what I'm 
trying to do below isn't right.

I've done a little poking around in projects written in rails I was able to 
find on github. I'm seeing a pattern where the functional tests focus on 
whether the controller invokes the right view and passes the right instance 
variables to that view - but not whether the view renders in any particular 
way. 

Is that the more rails-oriented way to go about things? And if so, why are 
assert_select and its relations so well developed?

-David

On Saturday, April 2, 2011 3:26:37 PM UTC-7, David Paschich wrote:
>
> Is it possible/feasible/advisable to test a partial view, on its own, 
> using 
> the default test mechanisms in Rails 3? I've been trying to figure out 
> how 
> to do this and not finding many good answers out there.  Maybe what 
> I'm 
> trying to do isn't "how it's done" in Rails, in which case please help 
> me 
> out. Below is a description of the problem, followed by some code to 
> illustrate what I'm talking about. 
>
> One of the views in my project is composed of a number of smaller 
> tables - 
> just the sort of thing where you'd like to factor those tables out of 
> the 
> single view into a number of partials, especially since I'd like to 
> potentially re-use those smaller tables in other views. 
>
> Right now, my functional test for the view has one long test method 
> with 
> sub-sections that test each partial. For two partials it's not bad; 
> but 
> eventually I'm going to have 7 or 8, and possibly let the user 
> customize 
> the page to show some but not others. 
>
> I'd really rather have 3 test methods here: one which ensures that 
> the 
> parent page renders both partials, and then one for each partial. And 
> then 
> if I use those partials in other views, I only have to make sure that 
> the 
> partials are rendered - and maybe add some test cases for the partials 
> themselves. 
>
> Below is the sample code, but so far the only definitive answer I've 
> been 
> able to find is "use Rspec". Is it really necessary to switch to a 
> whole 
> different testing framework just to do this? Or am I mis-using 
> functional 
> tests somehow? 
>
> I pledge to turn the best answer into a diff on the testing 
> RailsGuide. 
>
> Example code: 
>
> I've got a parent view like: 
>
> -- 
> <p>Some Preamble Stuff</p> 
> <%= render(:partial => "teamlist", :object => @season) %> 
> <%= render(:partial => "racerlist", :object => @season) %> 
> <p>Some Postamble Stuff</p> 
> -- 
>
> Each of those partials is something like: 
>
> -- 
> <table id="teamlist"> 
>  <% @season.teams.each do |team| %> 
>   <tr><td><%= team.name %></td></tr> 
>  <% end %> 
> </table> 
> -- 
>
> So how do I test that?  Right now, I have a functional test that looks 
> like: 
>
> -- 
> test "results page is correct" do 
>  get :results, :id => @season.id 
>  assert_response :success 
>
>  assert_select "table#teamlist" do 
>   # make sure team list is right 
>  end 
>
>  assert_select "table#racerlist" do 
>   # make sure racer list is right 
>  end 
> end 
> -- 
>
> But what I really want is: 
>
> -- 
> test "results page is correct" do 
>   get :results, :id = @season.id 
>   assert_response :success 
>   # assert the partials are rendered 
> end 
>
> test "teamlist is correct" do 
>   get :teamlist #or something 
>   asserts to make sure teamlist is correct 
> end 
>
> test "racerlist is correct" do 
>   render racerlist 
>   asserts to make sure racerlist is correct 
> end 
> -- 
>
> Anyone?

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to