我正在为我的待办事项列表应用程序使用 devise 和 simple_form。现在,我的 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 %>
我的 user.rb 看起来像这样:
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
现在,当我点击带有空 nick_name 字段的提交按钮时,我会收到一个弹出式警报。它不像普通的浏览器警报,我认为它是 HTML5 功能。我收到这条消息
Please fill out this field
作为空字段下方的弹出窗口。我已禁用 javascript,但它仍然显示相同的消息。这是我的 nick_name 输入字段:
<input class="string required" id="user_nick_name" name="user[nick_name]" required="required" size="50" type="text">
现在,当我删除模型中 nick_name 的存在验证时,不会出现此弹出窗口。当验证行被注释掉时,
<input class="string optional" 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 "Please fill out this field"
关于ruby-on-rails-3 - 使用 simple_form Rails 时测试 HTML 5 表单验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17384428/