views:

52

answers:

2

How can I confirm the absence of a HTML attribute in a Rails RSpec test?

I can verify that an input tag has a value attribute and that it is an empty string like so:

response.should have_tag("input[name=?][value=?]", "user[password]", "")
response.should have_tag("input[name=?][value=?]", "user[password_confirmation]", "")

But what I want to do is verify that my input fields do not have a value attribute at all (i.e., a blank field).

A: 

how about response.should_not have_tag("....")

Eimantas
How do I specify the lack of the value attribute though? It should have an input tag, just not a value attribute.
Jeff
+1  A: 

Figured it out:

response.should_not have_tag("input[name=?][value]", "user[password]")
response.should_not have_tag("input[name=?][value]", "user[password_confirmation]")

Just use a value attribute without the =? part. So if the input tag has a value attribute, regardless of what it contains, the test will fail.

Jeff
Four months later, I reap the benefits of your research. Thanks, Jeff.
Iain