开发者

Test for absence of an input tag's value attribute

开发者 https://www.devze.com 2023-01-04 07:53 出处:网络
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开发者_JS百科 it is an empty string like so:

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开发者_JS百科 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).


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.


There are numerous posts on various sites pointing out that have_tag returns an error that there's no such method as has_tag.

My workaround is to use have_selector:

    response.should have_selector("input#user_password", :content => "")

where user_password is the id attribute of the desired input tag (password field), which I found by looking at the source for my page (in this case, a signup page), and the pound sign delineates that I'm using the id field of the input tag.

I verified that this appears to work by changing the value for :content to "x", and my test failed.

Using rspec-rails 2.5.0 rspec 2.5.0


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

0

精彩评论

暂无评论...
验证码 换一张
取 消