Topic: Testing radio buttons...

Does anyone know of a good way to test that the proper radio button in a set is selected?  Test::Rails::ViewTestCase adds some custom assertions, but these don't seem to include anyway to easily add extra options/parameters to the the input tag.  There also is the assert_tag_in_form "primitive" that I could probably hack up, but that seems like over kill.

Anyone developed a useful way for testing this?

Last edited by NielsenE (2006-09-06 21:09:23)

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

Re: Testing radio buttons...

Have you looked into assert_select? It's included in the latest edge rails. You can get the plugin otherwise.

I haven't really tested it, but from the examples I think you can do this:

assert_select "input[type=radio][name=foo][value=bar][checked]"

This will look for an input tag that has each of those attributes. assert_select is a lot more powerful too, definitely worth looking into.

Last edited by ryanb (2006-09-07 13:21:43)

Railscasts - Free Ruby on Rails Screencasts

Re: Testing radio buttons...

Hmm, looks powerful, but I'm not sure I like it.  Partially because it conflicts with the more appropriately named "assert_select" from Test::Rails::ViewTestCase for testing select boxes.  And its syntax is kinda odd, while its more terse than assert_tag_in_form.  If I really felt like it, it might be interesting to look into making a parallel implementation of ViewTestCase that uses assert_select as its lower-level, non-exposed, work-horse assertion, while still having more usefully named and parametized functions built on top of it....

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

Re: Testing radio buttons...

The syntax is very similar to CSS selectors (hence the name). It may take some getting used to if you aren't familiar with that.

Railscasts - Free Ruby on Rails Screencasts

Re: Testing radio buttons...

I'm familiar with it; just getting spoiled by Ruby/Rails approach of making DSL's that still look like Ruby, so when another language is pulled in, it looks like a hack.

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

Re: Testing radio buttons...

I understand where you're coming from, but I don't see how a ruby-DSL would work for this kind of thing. assert_tag was an attempt at this, but assert_select feels more efficient. It's almost like regular expressions: there's a reason Ruby doesn't attempt to replace it with its own dsl, but instead accepts it and builds it right into the language.

Railscasts - Free Ruby on Rails Screencasts