Testing a partial on its own

Is it possible/feasible/advisable to test a partial view, on its own,
the default test mechanisms in Rails 3? I’ve been trying to figure out
to do this and not finding many good answers out there. Maybe what
trying to do isn’t “how it’s done” in Rails, in which case please help
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
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
sub-sections that test each partial. For two partials it’s not bad;
eventually I’m going to have 7 or 8, and possibly let the user
the page to show some but not others.

I’d really rather have 3 test methods here: one which ensures that
parent page renders both partials, and then one for each partial. And
if I use those partials in other views, I only have to make sure that
partials are rendered - and maybe add some test cases for the partials

Below is the sample code, but so far the only definitive answer I’ve
able to find is “use Rspec”. Is it really necessary to switch to a
different testing framework just to do this? Or am I mis-using
tests somehow?

I pledge to turn the best answer into a diff on the testing

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

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


assert_select “table#racerlist” do

make sure racer list is right


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


test “teamlist is correct” do
get :teamlist #or something
asserts to make sure teamlist is correct

test “racerlist is correct” do
render racerlist
asserts to make sure racerlist is correct


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
trying to do below isn’t right.

I’ve done a little poking around in projects written in rails I was able
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
variables to that view - but not whether the view renders in any

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


Hi David,

Regarding your outlook on functional testing, you’re basically correct.
goal of these is to isolate the code contained in the controller layer,
if you’re following skinny-controller/fat-model best practice, these
will generally be pretty basic – essentially, handle the setup and
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
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
because you’d need a controller method to prepare the state and trigger
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
anything useful for you there.

Thanks, Chris, for the confirmation. I’ll try to interpret the urge to
a view directly as a code smell; the point about view helper methods
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.


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