Topic: Testing Views: Yay or Nay?

My gut feeling is that testing views is taking the whole TDD thing too far. Unit, functional, integration and even AJAX tests make sense to me, but checking to see if a div has a certain class seems like overkill. Of course, this is coming from a guy who scoffed at the idea of testing months ago, so I realize I still have much to learn.

Do you guys test your views? If so then why

Re: Testing Views: Yay or Nay?

I don't test my views ... seems to be somewhat pointless ... one of the big issues being that just because an element has a class assigned doesn't mean the layout will actually work.

That said, I do use Selenium quite extensively and in my Selenium tests I will often check that certain IDs and classes exist. This is particularly useful for actions that trigger certain CSS changes (showing/hiding, that sort of thing).

Toby Hede
===================================================
FiniteStateMachine - Software Development for Social Networks
===================================================

Re: Testing Views: Yay or Nay?

I rarely test views. If your view is complex enough to require testing then you need to move logic out of the view.

However, I do usually test my helper methods which I recommend doing. They usually involve more complex logic which is worth testing.

BTW, the next Railscasts episode will be on testing controllers, so I think it kind of fits with this question.

Railscasts - Free Ruby on Rails Screencasts

Re: Testing Views: Yay or Nay?

Looking forward to that Railscast, Ryan.

Are you still using baked-in tests, or have you moved onto Rspec yet?

Re: Testing Views: Yay or Nay?

I've been using rspec for months now. I only use it on larger projects though.

Railscasts - Free Ruby on Rails Screencasts

Re: Testing Views: Yay or Nay?

Since switching over to RSpec 1.0.9 I started using story runner and as well I've began spec'in my views.

I honestly think that spec'in views is beneficial and necessary.

Just make your spec's are practical eg.

  it "should not show the game to annon users"
  it "should show the game name"
  it "should have an edit games link for the admin"
  it "should not have a edit games link for user"
  it "should how many players in the game"
  it "should how many much money you have left"

I think Spec'in views is part of the workflow, you start from the outside and work your way in:

Stories -> Views -> Controllers -> Models

Isn't TDD/BDD a way to prove that your application works as expected?
Why shouldn't you be proving what the user sees?

spec'in views can become granular but its up to the spec'r to make that call.

Last edited by OmenKing (2007-10-07 23:57:44)

Re: Testing Views: Yay or Nay?

At work, we've been porting old PHP projects to Rails and adding a bunch of features and what not... parts of the requirements is making sure all the old data works with the new system as well. So, if we have a smaller data port set, we will run each through the views it will be be shown in to make sure we don't get any exceptions thrown to our users. (Ie, trying to call a method on a attribute that is nil on an old data object, but could never be nil on a new one).
Other than that, I don't think its too useful.

Re: Testing Views: Yay or Nay?

Testing views is pointless unless you know 100% of exactly what you want in your view. Testing your view and then changing the view later on defeats the purpose of testing. Where I work all we do is controllers, helpers and models. We're always changing the views.

Re: Testing Views: Yay or Nay?

OmenKing wrote:

Isn't TDD/BDD a way to prove that your application works as expected?
Why shouldn't you be proving what the user sees?

That's a good point. If you test it properly then view tests can be flexible enough to not break when you don't intend them to.

But, even with this I don't find the effort is worth it. Testing the view is often more complicated than the view code itself which means you're more likely to make a mistake in the test then in the actual view. I can't tell you how many times I thought a view test was working but it really wasn't testing what I expected it to. Too often they break when I don't want them to and pass when they aren't supposed to. That was my experience with them anyway.

Railscasts - Free Ruby on Rails Screencasts