Rspec have_field 引发错误而 have_selector 返回成功 [英] Rspec have_field raises an error while have_selector return success
问题描述
在下面的示例中,3 个 have_field 规范失败,而 2 个 have_selector 通过.
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
在正在测试的模板中,我实际上有(浏览器中禁用了 js 支持):
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 规范按预期通过,但 have_field 未通过.为什么?
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
...
然后所有 5 个规格都变成绿色.很棒,但我不知道我的 nojs 规格有什么问题.
than all 5 specs become green. It's wonderful, but I have no damned idea, what is wrong with my nojs specs.
推荐答案
好吧,我的实验表明,不是简单地测试 have_field
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 }
然后测试通过.(小心!在类型设置中使用 :password,而不是 '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 }
这篇关于Rspec have_field 引发错误而 have_selector 返回成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!