views:

634

answers:

3

Hi,

I'm using Test/Unit with a standard rails 2.1 project. I would like to be able to test Partial Views in isolation from any particular controller / action.

It seemed as though ZenTest's Test::Rails::ViewTestCase would help, but I couldn't get it working, similarly with view_test http://www.continuousthinking.com/tags/view_test

Most of the stuff Google turns up seems quite out of date, so I'm guessing doesn't really work with Rails 2.1

Any help with this much appreciated.

Thanks, Roland

A: 

Testing a view without the controller code is a dangerous thing. Your tests might pass but your application might throw an error. Always test against real life situations not artificial ones.

TonyLa
> Your tests might pass but your application might throw an error.This is always true.I think you maybe missing what I am trying to achieve here. View testing is a small part of an overall testing strategy
Roland
+2  A: 

We're using RSpec in our Rails 2.1 project, and we can do this sort of thing:

describe "/posts/_form" do
  before do
    render :partial => "posts/form"
  end
  it "says hello" do
    response.should match(/hello/i)
  end
  it "renders a form" do
    response.should have_tag("form")
  end
end

However I don't know how much of that you can do with the vanilla Rails testing apparatus.

Sam Stokes
yup - unfortunately I have too many test/unit tests to switch over to rspec for this project - although I've moved to rspec in current apps partially because it seems to support this fined-grained testing
Roland
+2  A: 

Found this which may be relevant:

http://broadcast.oreilly.com/2008/10/testing-rails-partials.html

Roland