[rspec-users] Speccing the existance of an unchecked checkbox

Matt Wynne matt at mattwynne.net
Wed Sep 3 09:38:11 EDT 2008


On 3 Sep 2008, at 14:31, Bart Zonneveld wrote:

>
> On 3 sep 2008, at 15:28, David Chelimsky wrote:
>
>> On Wed, Sep 3, 2008 at 6:56 AM, Bart Zonneveld  
>> <loop at superinfinite.com> wrote:
>>> Hey list,
>>>
>>> I found myself trying to verify there are some non-checked  
>>> checkboxes in a
>>> template today, and am kinda stumped how to do it :).
>>> A checked checkbox is easy,
>>> have_tag('input[type=checkbox][checked=checked]). But, an  
>>> unchecked checkbox
>>> hasn't got the checked attribute at all. And as it so happens, I  
>>> want to
>>> test for a number of checked, and a number of unchecked  
>>> checkboxes. So, just
>>> testing for input[type=checkbox] would return the number of  
>>> checked *and*
>>> unchecked checkboxes..
>>>
>>> any ideas?
>>
>> Let's say you want 10 checked and 5 unchecked. You could do this:
>>
>> response.should have_tag("input[type=checkbox]", 15)
>> response.should have_tag("input[type=checkbox][checked=checked]", 10)
>>
>> It's not perfectly expressive, but a good example name would help:
>>
>> it "should have 15 checkboxes, 10 checked, 5 unchecked" do
>>  render "/path/to/file"
>>  response.should have_tag("input[type=checkbox]", 15)
>>  response.should have_tag("input[type=checkbox][checked=checked]",  
>> 10)
>> end
>>
>> WDYT?
>
> Yeah, that's how I ended up doing it. Googled around a bit, and  
> apparently you cannot do something like input[type=checkbox] 
> [checked!=checked].
> I'll file a ticket for assert_select.
>
> thanks,
> bartz

I guess you could also consider putting response.body into a HPricot  
object and doing something fancy with that.

IIRC, asset_select isn't using HPricot which seems like a shame - I  
seem to remember barking up this tree myself a few weeks ago...

cheers,
Matt


More information about the rspec-users mailing list