当have_selector返回成功时,Rspec have_field引发错误

在下面的示例中,3个has_field规范失败,而2个have_selector传递。

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 

在正在测试的模板中,我实际上已经(在浏览器中禁用了js支持):

   

have_selector规范按预期传递,但has_field不传递。 为什么?

更有趣的是我将一个例子更改为:

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

比所有5个规格都变绿了。 这太好了,但我没有该死的想法,我的nojs规格出了什么问题。

好吧,我的实验表明,而不是简单地测试have_field

 it { should have_field "user_password" } 

我必须明确设置字段类型

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

然后测试通过。 (小心!使用:密码,而不是类型设置中的’密码’)。

如果你必须检查密码没有值,它可以像往常一样

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