Testing a partial on its own

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:

<% @season.teams.each do |team| %> <% end %>
<%= 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?

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

Hi David,

Regarding your outlook on functional testing, you’re basically correct.
The
goal of these is to isolate the code contained in the controller layer,
and
if you’re following skinny-controller/fat-model best practice, these
tests
will generally be pretty basic – essentially, handle the setup and
action
performed, and then check the variables set, the session and flash state
changes, the HTTP response, and the view rendered or redirect sent.

As far as your question about testing individual partials, I don’t
personally do a ton of testing on the view level as such. I will scan
my
views every so often to see if there’s a bit of logic that would be a
candidate for testing and refactor those into helper methods wherever
possible. But as far as I know, there’s not really an easy way to test
individual partials outside the context of the templates that render
them
because you’d need a controller method to prepare the state and trigger
the
call to render them.

If this is something that interests you, I saw a library called Cells (
http://cells.rubyforge.org/) a few months back that creates something
like a
miniature MVC stack for view fragments. I believe it’s possible to test
these independently, so you might want to check it out and see if
there’s
anything useful for you there.

Thanks, Chris, for the confirmation. I’ll try to interpret the urge to
test
a view directly as a code smell; the point about view helper methods
being
testable is a good one. I’ll also check out that Cells package - sounds
like it may match how I’m thinking about my application more closely.

-David

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs