sim*_*mha 7 capybara ruby-on-rails-3 simple-form
我正在为我的待办事项列表应用程序使用devise和simple_form.现在,我的用户/ 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 %>
Run Code Online (Sandbox Code Playgroud)
我的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
Run Code Online (Sandbox Code Playgroud)
现在,当我点击带有空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">
Run Code Online (Sandbox Code Playgroud)
现在,当我在模型中删除nick_name的状态验证时,此弹出窗口不会出现.当验证行被注释掉时,
<input class="string optional" id="user_nick_name" name="user[nick_name]" size="50" type="text">
Run Code Online (Sandbox Code Playgroud)
simple_form在幕后做了一些魔术吗?
由于这个弹出窗口没有显示任何html代码的痕迹,如何在capybara/rspec中测试这个验证?
小智 5
由于您正在使用该required="required"属性,因此这会触发 HTML5 验证。
要测试这一点:
message = find("#user_nick_name").native.attribute("validationMessage")
expect(message).to eq "Please fill out this field"
required="required"实际上,您可以通过使用以下代码示例查找 html 属性来测试它:
expect(page).to have_xpath("//input[@required='required']")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2042 次 |
| 最近记录: |