Topic: Brittleness of View test cases...

(Given the derth of replies to my Test::Rails thread, this might be a poor place to ask, but...) 

I'm moving into finally developing some views under TDD now that I can't put it off anymore (4 completed model/controller pairs for the first interation).  The surface API of the ViewTestCase in Test::Rails appears very good for driving the embedded Ruby content in the views; however I'm finding that the lack of access to the routing infrastructure is leading me to create brittle tests.

Ie the assertions like "assert_post_form", "assert_links_to" and related all want to take a constructed URL (ie /controller/action[/id]?) instead of any other the other more common forms of specifying a URL.  Thus the actual format of the URL is hard-coded into a view test; while the creation of the URL in the view/helper is normally off-loaded to the routes infrastructure.

I already have exercised the applications routing in specific tests in the controller test suite and that I feel should be the single point of route testing.  Now if I update a route, I'll also need to update all the view tests to avoid the avalanache of failures, not so good.  I'm surprised that the ZenTest module would have overlooked this problem, so I suspect I'm missing something.  The ViewTestCase code does hard-code the urls though.

Is there a reason why hard-coding the URLs in the view tests is a good thing?

(I understand, and appreciate, that separation between View and Controller tests that Test::Rails makes.  And I know that the route generating code is buried in the controller layer, so I understand why giving the View Test tools access to it might be a little odd, but this negative I'm finding seems worse....)

My RoR journey  -- thoughts on learning RoR and lessons learned in applying TDD and agile practices.

Re: Brittleness of View test cases...

If I understand, the same problem exists in the conventional Rails tests, you can get around it by putting this in the helper:

  def url_for(params)
    @controller.url_for(params.merge({ :only_path => true }))
  end

You could probably do something similar for url generation in view tests, as you know from the other thread, I haven't touched Test::Rails yet, so I'm hazarding guesses.

Of course, the url_for in your views will always match the url_for in your tests, so if they both do something crazy that you don't want, they will still match. But if url_for/routes are already tested, I'm not convinced that's a problem either.

Re: Brittleness of View test cases...

The problem is that @controller isn't accessible in a Test::Rails ViewTestCase.

My RoR journey  -- thoughts on learning RoR and lessons learned in applying TDD and agile practices.

Re: Brittleness of View test cases...

NielsenE wrote:

The problem is that @controller isn't accessible in a Test::Rails ViewTestCase.

I was suggesting you artificially instantiate it, but after a couple minutes of poking around, how isn't obvious to me, so perhaps that isn't a good suggestion. smile