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:
–
Some Preamble Stuff
<%= render(:partial => "teamlist", :object => @season) %> <%= render(:partial => "racerlist", :object => @season) %>Some Postamble Stuff
--Each of those partials is something like:
–
<%= team.name %> |
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?