Rspec have_field raises an error while have_selector return success

血红的双手。 提交于 2020-01-15 23:35:24

问题


In the example below 3 have_field specs fail, while 2 have_selector pass.

describe "without js support", js: false do
  subject { page }      

  it { should have_selector "form label[for='user_password']", text: "Password" }
  it { should have_selector "form input#user_password[name='user[password]'][type='password'][required]" }
  it { should have_field "user_password" }  # check by field id
  it { should have_field "user[password]" } # check by field name
  it { should have_field "Password" }       # check by field label
end

In the template being tested I actually have (js support disabled in browser):

<label for="user_password" id="label_password">Password</label>
<input id="user_password" name="user[password]" required="required" type="password" />

have_selector specs pass as expected, but have_field aren't. Why?

Even more interesting is after I change an example to:

describe "with js support", js: true" do
...

than all 5 specs become green. It's wonderful, but I have no damned idea, what is wrong with my nojs specs.


回答1:


Well, my experiments shows that instead of simply testing have_field

it { should have_field "user_password" }

I have to explicitly set field type

it { should have_field "user_password", :type => :password }

Then the test passes. (Be careful! Use :password, not 'password' in type setting).

And if you have to check password hasn't value, it can be done as usual

it { should have_field "user_password", :type => :password, :with => nil }


来源:https://stackoverflow.com/questions/14095628/rspec-have-field-raises-an-error-while-have-selector-return-success

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!