使用simple_form Rails时测试HTML 5表单验证 [英] Testing HTML 5 form validations when using simple_form Rails
问题描述
我正在将devise和simple_form用于待办事项列表应用程序。现在,我为我的用户提供了以下代码/edit.html.erb
<%= content_for:title,'Edit个人资料'%>
< h2>编辑个人资料< / h2>
<%= simple_form_for current_user,类别:水平表单| f | %>
<%= f.input:昵称%>
<%= f。提交更新个人资料%>
<%end%>
我的user.rb看起来像这样:
类用户< ActiveRecord :: Base
$ p已经验证过的其他字段$ p>
设计:database_authenticatable,:registerable,
:recoverable,:rememberable,:trackable,:validatable
attr_accessible:email,:nick_name,:password,:password_confirmation,:remember_me
validates:nick_name,状态:true#已经通过设计
has_many:lists,dependent::destroy
end
现在,当我单击带有空nick_name字段的Submit按钮时,我会弹出一个警报。它不像普通的浏览器警报,我认为它具有HTML5功能。我收到此消息
请填写此字段
作为空白字段下方的弹出窗口。我已禁用javascript,但仍显示相同的消息。
这是我的昵称输入字段:< input class = string required id = user_nick_name name = user [nick_name]必需=必需 size = 50 type = text>
现在,当我删除模型中nick_name的状态验证时,不会出现此弹出窗口。当验证行被注释掉时,
< input class = string可选 id = user_nick_name name = user [ nick_name] size = 50 type = text>
simple_form是在幕后做魔术吗?
由于此弹出窗口不显示任何html代码痕迹,因此如何在capybara / rspec中测试此验证?
解决方案由于您使用的是
required = required
属性,因此会触发HTML5验证。
要对此进行测试:
message = find(#user_nick_name)。native.attribute( validationMessage )
Expect(message).to eq请填写此字段
I'm using devise and simple_form for my todo list app . Now , I have the following code for my users/edit.html.erb
<%= content_for :title,'Edit profile' %> <h2>Edit profile</h2> <%= simple_form_for current_user, class: 'form-horizontal' do |f| %> <%= f.input :nick_name %> <%= f.submit 'Update profile' %> <% end %>
My user.rb looks like this :
class User < ActiveRecord::Base devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable attr_accessible :email,:nick_name, :password, :password_confirmation, :remember_me validates :nick_name, presence: true # the other fields already validated by devise has_many :lists , dependent: :destroy end
Now, when I click on submit button with an empty nick_name field ,I get a popup kind of alert . It's not like a normal browser alert ,I think its a HTML5 feature . I get this message
Please fill out this field
as a popup below the empty field . I have disabled javascript, but it still shows the same message . This is my nick_name input field :<input class="string required" id="user_nick_name" name="user[nick_name]" required="required" size="50" type="text">
Now, when I remove the presence validation for nick_name in my model , this popup doesn't appear .When validation line is commented out ,
<input class="string optional" id="user_nick_name" name="user[nick_name]" size="50" type="text">
Is simple_form doing some behind the scenes magic ?
Since this popup doesnt show any trace of html code , How to test for this validation in capybara/rspec ?
解决方案Since you are using the
required="required"
attribute, this triggers an HTML5 validation.To test this:
message = find("#user_nick_name").native.attribute("validationMessage") expect(message).to eq "Please fill out this field"
这篇关于使用simple_form Rails时测试HTML 5表单验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!