Roland
Roland

Reputation: 1196

Testing rails partial views standalone

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

Upvotes: 7

Views: 4148

Answers (3)

Roland
Roland

Reputation: 1196

Found this which may be relevant:

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

Upvotes: 1

Sam Stokes
Sam Stokes

Reputation: 14807

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.

Upvotes: 6

TonyLa
TonyLa

Reputation: 734

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.

Upvotes: -3

Related Questions